If you have a
Traversable instance of a record, you can load
and store all elements, that are accessible by
Traversable
methods. We treat the record like an array, that is we assume, that
all elements have the same size and alignment.
Example:
import Foreign.Storable.Traversable as Store
data Stereo a = Stereo {left, right :: a}
instance Functor Stereo where
fmap = Trav.fmapDefault
instance Foldable Stereo where
foldMap = Trav.foldMapDefault
instance Traversable Stereo where
sequenceA ~(Stereo l r) = liftA2 Stereo l r
instance (Storable a) => Storable (Stereo a) where
sizeOf = Store.sizeOf
alignment = Store.alignment
peek = Store.peek (error "instance Traversable Stereo is lazy, so we do not provide a real value here")
poke = Store.poke
You would certainly not define the
Traversable and according
instances just for the implementation of the
Storable instance,
but there are usually similar applications where the
Traversable instance is useful.