Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module is intended to be imported qualified to avoid name clashes with Prelude. E.g.:
import FlatBuffers.Vector (Vector, WriteVector) import qualified FlatBuffers.Vector as Vector
Synopsis
- class WriteVectorElement a where
- data WriteVector a
- fromFoldable :: Foldable f => Int32 -> f a -> WriteVector a
- fromFoldable' :: WriteVectorElement a => Foldable f => f a -> WriteVector a
- fromList :: WriteVectorElement a => Int32 -> [a] -> WriteVector a
- fromList' :: WriteVectorElement a => [a] -> WriteVector a
- singleton :: WriteVectorElement a => a -> WriteVector a
- empty :: WriteVectorElement a => WriteVector a
- class VectorElement a where
Creating a vector
class WriteVectorElement a where Source #
data WriteVector a Source #
A vector to be written to a flatbuffer.
:: Foldable f | |
=> Int32 |
|
-> f a |
|
-> WriteVector a |
Constructs a flatbuffers vector.
If n
is larger than the length of xs
, this will result in a malformed buffer.
If n
is smaller than the length of xs
, all elements of xs
will still be written to the buffer,
but the client will only be able to read the first n
elements.
Note: fromFoldable
asks for the collection's length to be passed in as an argument rather than use Foldable.length
because:
Foldable.length
is often O(n), and in some use cases there may be a better way to know the collection's length ahead of time.- Calling
Foldable.length
insidefromFoldable
can inhibit some fusions which would otherwise be possible.
Instances
fromFoldable' :: WriteVectorElement a => Foldable f => f a -> WriteVector a Source #
Convenience function, equivalent to:
fromFoldable' xs = fromFoldable (fromIntegral (Foldable.length xs)) xs
In some cases it may be slower than using fromFoldable
directly.
fromList :: WriteVectorElement a => Int32 -> [a] -> WriteVector a Source #
fromFoldable
specialized to list
fromList' :: WriteVectorElement a => [a] -> WriteVector a Source #
fromFoldable'
specialized to list
singleton :: WriteVectorElement a => a -> WriteVector a Source #
Creates a flatbuffers vector with a single element
empty :: WriteVectorElement a => WriteVector a Source #
Creates an empty flatbuffers vector
Reading a vector
class VectorElement a where Source #
length :: Vector a -> Either ReadError Int32 Source #
Returns the size of the vector.
index :: Vector a -> Int32 -> Either ReadError a Source #
Returns the item at the given index.
If the given index is negative or too large, an error
is thrown.
unsafeIndex :: Vector a -> Int32 -> Either ReadError a Source #
Returns the item at the given index without performing the bounds check.
Given an invalid index, unsafeIndex
will likely read garbage data or return a ReadError
.
In the case of Vector Word8
, using a negative index carries the same risks as unsafeIndex
(i.e. reading from outside the buffer's boundaries).
toList :: Vector a -> Either ReadError [a] Source #
Converts the vector to a list.