Created
June 7, 2013 21:59
-
-
Save acowley/5732709 to your computer and use it in GitHub Desktop.
Demonstrate building a ByteString from heterogenous Storable types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ScopedTypeVariables #-} | |
import Data.Binary.Builder | |
import Data.Binary.Builder.Internal | |
import Foreign.Ptr (castPtr) | |
import Foreign.Storable (Storable(sizeOf,poke)) | |
-- For testing | |
import Linear | |
import Data.ByteString.Lazy (ByteString) | |
import Data.Monoid ((<>)) | |
store :: forall a. Storable a => a -> Builder | |
store x = writeN (sizeOf (undefined::a)) (flip poke x . castPtr) | |
-- Testing | |
data Foo = Foo (V2 Float) (V3 Double) | |
fooBytes :: Foo -> ByteString | |
fooBytes (Foo bar baz) = toLazyByteString $ store bar <> store baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment