Data.ByteArray.Sized

memory-0.18.0: memory and related abstraction stuff

License | BSD-style
Maintainer | Nicolas Di Prima <nicolas@primetype.co.uk>
Stability | stable
Portability | Good
Safe Haskell | None
Language | Haskell2010

Data.ByteArray.Sized

Contents

- ByteArrayN operators

Description

Synopsis

- class (ByteArrayAccess c, KnownNat n) => ByteArrayN (n :: Nat) c | c -> n where
  - allocRet :: Proxy n -> (Ptr p -> IO a) -> IO (a, c)
- data SizedByteArray (n :: Nat) ba
- unSizedByteArray :: SizedByteArray n ba -> ba
- sizedByteArray :: forall (n :: Nat) ba. (KnownNat n, ByteArrayAccess ba) => ba -> Maybe (SizedByteArray n ba)
- unsafeSizedByteArray :: forall (n :: Nat) ba. (ByteArrayAccess ba, KnownNat n) => ba -> SizedByteArray n ba
- alloc :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> IO ba
- create :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> IO ba
- allocAndFreeze :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> ba
- unsafeCreate :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> ba
- inlineUnsafeCreate :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> ba
- empty :: ByteArrayN 0 ba => ba
- pack :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n) => ListN n Word8 -> ba
- unpack :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n, NatWithinBound Int n, ByteArrayAccess ba) => ba -> ListN n Word8
- cons :: forall (ni :: Nat) (no :: Nat) bi bo. (ByteArrayN ni bi, ByteArrayN no bo, ByteArrayAccess bi, KnownNat ni, KnownNat no, (ni + 1) ~ no) => Word8 -> bi -> bo
- snoc :: forall bi bo (ni :: Nat) (no :: Nat). (ByteArrayN ni bi, ByteArrayN no bo, ByteArrayAccess bi, KnownNat ni, KnownNat no, (ni + 1) ~ no) => bi -> Word8 -> bo
- xor :: forall (n :: Nat) a b c. (ByteArrayN n a, ByteArrayN n b, ByteArrayN n c, ByteArrayAccess a, ByteArrayAccess b, KnownNat n) => a -> b -> c
- index :: forall (n :: Nat) (na :: Nat) ba. (ByteArrayN na ba, ByteArrayAccess ba, KnownNat na, KnownNat n, n <= na) => ba -> Proxy n -> Word8
- splitAt :: forall (nblhs :: Nat) (nbi :: Nat) (nbrhs :: Nat) bi blhs brhs. (ByteArrayN nbi bi, ByteArrayN nblhs blhs, ByteArrayN nbrhs brhs, ByteArrayAccess bi, KnownNat nbi, KnownNat nblhs, KnownNat nbrhs, nblhs <= nbi, (nbrhs + nblhs) ~ nbi) => bi -> (blhs, brhs)
- take :: forall (nbo :: Nat) (nbi :: Nat) bi bo. (ByteArrayN nbi bi, ByteArrayN nbo bo, ByteArrayAccess bi, KnownNat nbi, KnownNat nbo, nbo <= nbi) => bi -> bo
- drop :: forall (n :: Nat) (nbi :: Nat) (nbo :: Nat) bi bo. (ByteArrayN nbi bi, ByteArrayN nbo bo, ByteArrayAccess bi, KnownNat n, KnownNat nbi, KnownNat nbo, (nbo + n) ~ nbi) => Proxy n -> bi -> bo
- append :: forall (nblhs :: Nat) (nbrhs :: Nat) (nbout :: Nat) blhs brhs bout. (ByteArrayN nblhs blhs, ByteArrayN nbrhs brhs, ByteArrayN nbout bout, ByteArrayAccess blhs, ByteArrayAccess brhs, KnownNat nblhs, KnownNat nbrhs, KnownNat nbout, (nbrhs + nblhs) ~ nbout) => blhs -> brhs -> bout
- copy :: forall (n :: Nat) bs1 bs2 p. (ByteArrayN n bs1, ByteArrayN n bs2, ByteArrayAccess bs1, KnownNat n) => bs1 -> (Ptr p -> IO ()) -> IO bs2
- copyRet :: forall (n :: Nat) bs1 bs2 p a. (ByteArrayN n bs1, ByteArrayN n bs2, ByteArrayAccess bs1, KnownNat n) => bs1 -> (Ptr p -> IO a) -> IO (a, bs2)
- copyAndFreeze :: forall (n :: Nat) bs1 bs2 p. (ByteArrayN n bs1, ByteArrayN n bs2, ByteArrayAccess bs1, KnownNat n) => bs1 -> (Ptr p -> IO ()) -> bs2
- replicate :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n) => Word8 -> ba
- zero :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n) => ba
- convert :: forall (n :: Nat) bin bout. (ByteArrayN n bin, ByteArrayN n bout, KnownNat n) => bin -> bout
- fromByteArrayAccess :: forall (n :: Nat) bin bout. (ByteArrayAccess bin, ByteArrayN n bout, KnownNat n) => bin -> Maybe bout
- unsafeFromByteArrayAccess :: forall (n :: Nat) bin bout. (ByteArrayAccess bin, ByteArrayN n bout, KnownNat n) => bin -> bout

# Documentation

class (ByteArrayAccess c, KnownNat n) => ByteArrayN (n :: Nat) c | c -> n where #

Type class to emulate exactly the behaviour of `ByteArray` but with a known length at compile time

Methods

allocRet :: Proxy n -> (Ptr p -> IO a) -> IO (a, c) #

just like `allocRet` but with the size at the type level

#### Instances

Instances details

(KnownNat n, ByteArray ba) => ByteArrayN n (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods allocRet :: Proxy n -> (Ptr p -> IO a) -> IO (a, SizedByteArray n ba) #
(ByteArrayAccess (BlockN n ty), PrimType ty, KnownNat n, Countable ty n, KnownNat nbytes, nbytes ~ (PrimSize ty * n)) => ByteArrayN nbytes (BlockN n ty) # |
Instance details Defined in Data.ByteArray.Sized Methods allocRet :: Proxy nbytes -> (Ptr p -> IO a) -> IO (a, BlockN n ty) #

data SizedByteArray (n :: Nat) ba #

Wrapper around any collection type with the size as type parameter

#### Instances

Instances details

(KnownNat n, ByteArray ba) => ByteArrayN n (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods allocRet :: Proxy n -> (Ptr p -> IO a) -> IO (a, SizedByteArray n ba) #
NormalForm ba => NormalForm (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods toNormalForm :: SizedByteArray n ba -> () #
Show ba => Show (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods showsPrec :: Int -> SizedByteArray n ba -> ShowS # show :: SizedByteArray n ba -> String # showList :: [SizedByteArray n ba] -> ShowS #
Eq ba => Eq (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods (==) :: SizedByteArray n ba -> SizedByteArray n ba -> Bool # (/=) :: SizedByteArray n ba -> SizedByteArray n ba -> Bool #
Ord ba => Ord (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods compare :: SizedByteArray n ba -> SizedByteArray n ba -> Ordering # (<) :: SizedByteArray n ba -> SizedByteArray n ba -> Bool # (<=) :: SizedByteArray n ba -> SizedByteArray n ba -> Bool # (>) :: SizedByteArray n ba -> SizedByteArray n ba -> Bool # (>=) :: SizedByteArray n ba -> SizedByteArray n ba -> Bool # max :: SizedByteArray n ba -> SizedByteArray n ba -> SizedByteArray n ba # min :: SizedByteArray n ba -> SizedByteArray n ba -> SizedByteArray n ba #
(ByteArrayAccess ba, KnownNat n) => ByteArrayAccess (SizedByteArray n ba) # |
Instance details Defined in Data.ByteArray.Sized Methods length :: SizedByteArray n ba -> Int # withByteArray :: SizedByteArray n ba -> (Ptr p -> IO a) -> IO a # copyByteArrayToPtr :: SizedByteArray n ba -> Ptr p -> IO () #

unSizedByteArray :: SizedByteArray n ba -> ba #

sizedByteArray :: forall (n :: Nat) ba. (KnownNat n, ByteArrayAccess ba) => ba -> Maybe (SizedByteArray n ba) #

create a `SizedByteArray` from the given `ByteArrayAccess` if the size is the same as the target size.

unsafeSizedByteArray :: forall (n :: Nat) ba. (ByteArrayAccess ba, KnownNat n) => ba -> SizedByteArray n ba #

just like the `sizedByteArray` function but throw an exception if the size is invalid.

# ByteArrayN operators

alloc :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> IO ba #

Allocate a new bytearray of specific size, and run the initializer on this memory

create :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> IO ba #

Allocate a new bytearray of specific size, and run the initializer on this memory

allocAndFreeze :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> ba #

similar to `allocN` but hide the allocation and initializer in a pure context

unsafeCreate :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> ba #

similar to `createN` but hide the allocation and initializer in a pure context

inlineUnsafeCreate :: forall (n :: Nat) ba p. (ByteArrayN n ba, KnownNat n) => (Ptr p -> IO ()) -> ba #

empty :: ByteArrayN 0 ba => ba #

Create an empty byte array

pack :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n) => ListN n Word8 -> ba #

Pack a list of bytes into a bytearray

unpack :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n, NatWithinBound Int n, ByteArrayAccess ba) => ba -> ListN n Word8 #

Un-pack a bytearray into a list of bytes

cons :: forall (ni :: Nat) (no :: Nat) bi bo. (ByteArrayN ni bi, ByteArrayN no bo, ByteArrayAccess bi, KnownNat ni, KnownNat no, (ni + 1) ~ no) => Word8 -> bi -> bo #

prepend a single byte to a byte array

snoc :: forall bi bo (ni :: Nat) (no :: Nat). (ByteArrayN ni bi, ByteArrayN no bo, ByteArrayAccess bi, KnownNat ni, KnownNat no, (ni + 1) ~ no) => bi -> Word8 -> bo #

append a single byte to a byte array

xor :: forall (n :: Nat) a b c. (ByteArrayN n a, ByteArrayN n b, ByteArrayN n c, ByteArrayAccess a, ByteArrayAccess b, KnownNat n) => a -> b -> c #

Create a xor of bytes between a and b.

the returns byte array is the size of the smallest input.

index :: forall (n :: Nat) (na :: Nat) ba. (ByteArrayN na ba, ByteArrayAccess ba, KnownNat na, KnownNat n, n <= na) => ba -> Proxy n -> Word8 #

return a specific byte indexed by a number from 0 in a bytearray

unsafe, no bound checking are done

splitAt :: forall (nblhs :: Nat) (nbi :: Nat) (nbrhs :: Nat) bi blhs brhs. (ByteArrayN nbi bi, ByteArrayN nblhs blhs, ByteArrayN nbrhs brhs, ByteArrayAccess bi, KnownNat nbi, KnownNat nblhs, KnownNat nbrhs, nblhs <= nbi, (nbrhs + nblhs) ~ nbi) => bi -> (blhs, brhs) #

Split a bytearray at a specific length in two bytearray

take :: forall (nbo :: Nat) (nbi :: Nat) bi bo. (ByteArrayN nbi bi, ByteArrayN nbo bo, ByteArrayAccess bi, KnownNat nbi, KnownNat nbo, nbo <= nbi) => bi -> bo #

Take the first `n` byte of a bytearray

drop :: forall (n :: Nat) (nbi :: Nat) (nbo :: Nat) bi bo. (ByteArrayN nbi bi, ByteArrayN nbo bo, ByteArrayAccess bi, KnownNat n, KnownNat nbi, KnownNat nbo, (nbo + n) ~ nbi) => Proxy n -> bi -> bo #

drop the first `n` byte of a bytearray

append :: forall (nblhs :: Nat) (nbrhs :: Nat) (nbout :: Nat) blhs brhs bout. (ByteArrayN nblhs blhs, ByteArrayN nbrhs brhs, ByteArrayN nbout bout, ByteArrayAccess blhs, ByteArrayAccess brhs, KnownNat nblhs, KnownNat nbrhs, KnownNat nbout, (nbrhs + nblhs) ~ nbout) => blhs -> brhs -> bout #

append one bytearray to the other

copy :: forall (n :: Nat) bs1 bs2 p. (ByteArrayN n bs1, ByteArrayN n bs2, ByteArrayAccess bs1, KnownNat n) => bs1 -> (Ptr p -> IO ()) -> IO bs2 #

Duplicate a bytearray into another bytearray, and run an initializer on it

copyRet :: forall (n :: Nat) bs1 bs2 p a. (ByteArrayN n bs1, ByteArrayN n bs2, ByteArrayAccess bs1, KnownNat n) => bs1 -> (Ptr p -> IO a) -> IO (a, bs2) #

Similar to `copy` but also provide a way to return a value from the initializer

copyAndFreeze :: forall (n :: Nat) bs1 bs2 p. (ByteArrayN n bs1, ByteArrayN n bs2, ByteArrayAccess bs1, KnownNat n) => bs1 -> (Ptr p -> IO ()) -> bs2 #

Similiar to `copy` but expect the resulting bytearray in a pure context

replicate :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n) => Word8 -> ba #

Create a bytearray of a specific size containing a repeated byte value

zero :: forall (n :: Nat) ba. (ByteArrayN n ba, KnownNat n) => ba #

Create a bytearray of a specific size initialized to 0

convert :: forall (n :: Nat) bin bout. (ByteArrayN n bin, ByteArrayN n bout, KnownNat n) => bin -> bout #

Convert a bytearray to another type of bytearray

fromByteArrayAccess :: forall (n :: Nat) bin bout. (ByteArrayAccess bin, ByteArrayN n bout, KnownNat n) => bin -> Maybe bout #

Convert a ByteArrayAccess to another type of bytearray

This function returns nothing if the size is not compatible

unsafeFromByteArrayAccess :: forall (n :: Nat) bin bout. (ByteArrayAccess bin, ByteArrayN n bout, KnownNat n) => bin -> bout #

Convert a ByteArrayAccess to another type of bytearray