purescript-contrib/purescript-arraybuffer

export Typed.part'

jamesdbrock opened this issue · 6 comments

The Typed.part function takes an Index as its second argument, in units of the number of typed elements. What if we want to make an ArrayView which doesn't start in the ArrayBuffer at a typed element boundary?

https://github.com/jacereda/purescript-arraybuffer/blob/7f90bc44247dcbeab70caef5d373833286468266/src/Data/ArrayBuffer/Typed.purs#L137-L137

I think this library should also export the Typed.part' function, which is the same but with a ByteOffset type for the second argument.

@jamesdbrock could you propose an expected type signature? I'm not sure if I completely understand what you're asking for.

Oh, I've been assuming that I could work with DataView and ArrayView on the same ArrayBuffer, but that's not really how this is supposed to work, is it? DataView is for network transport. ArrayView is for buffers that stay on the local machine.

I can see now that probably no-one would ever want to construct a non-aligned ArrayView.

I mean, it might make sense. I personally share an ArrayBuffer between multiple DataViews and ArrayViews often. ArrayBuffer is just a reference to some mutable state, like an STRef that you can't read or write to, because it's not formatted. DataView gives you manual set and read functions, based on specific byte-wise indexes, but only with one value at a time. However, an ArrayView gives you a typed interface to an ArrayBuffer for the whole thing, acting as a typed array for a specific numeric type.

If I recall correctly, there is a way to slice into ArrayBuffers, giving you a new one to the same piece of memory, so you could offset manually like that, but I don't actually see an issue with what you're proposing. The thing is, though, this library should be a lightweight layer on top of the native interface javascript provides. I'll have to dig a little, but I'll try to get this idea fleshed out.

Cool, thanks for maintaining this library, it's really nice.

Wow I feel silly, I forgot this is actually a component of the library's FFI already x_x I'm exposing it now, sorry for the delay!