module Data.Iteratee.Base.ReadableChunk (
ReadableChunk (..)
)
where
import Prelude hiding (head, tail, dropWhile, length, splitAt )
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.Word
import Control.Monad.IO.Class
import Foreign.C
import Foreign.Ptr
import Foreign.Storable
import Foreign.Marshal.Array
class (Storable el) => ReadableChunk s el | s -> el where
readFromPtr ::
MonadIO m =>
Ptr el
-> Int
-> m s
instance ReadableChunk [Char] Char where
readFromPtr buf l = liftIO $ peekCAStringLen (castPtr buf, l)
instance ReadableChunk [Word8] Word8 where
readFromPtr buf l = liftIO $ peekArray l buf
instance ReadableChunk [Word16] Word16 where
readFromPtr buf l = liftIO $ peekArray l buf
instance ReadableChunk [Word32] Word32 where
readFromPtr buf l = liftIO $ peekArray l buf
instance ReadableChunk [Word] Word where
readFromPtr buf l = liftIO $ peekArray l buf
instance ReadableChunk B.ByteString Word8 where
readFromPtr buf l = liftIO $ B.packCStringLen (castPtr buf, l)
instance ReadableChunk L.ByteString Word8 where
readFromPtr buf l = liftIO $
return . L.fromChunks . (:[]) =<< readFromPtr buf l