Skip to content

Commit e7df4e8

Browse files
committed
Add functions for recovering length from ByteArrayN
1 parent a156990 commit e7df4e8

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/Data/Bytes/Indexed.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
{-# language DataKinds #-}
2+
{-# language MagicHash #-}
23
{-# language TypeOperators #-}
34

45
module Data.Bytes.Indexed
56
( ByteArrayN
67
, append
8+
, length
9+
, length#
710
) where
811

12+
import Prelude hiding (length)
13+
14+
import Data.Primitive (ByteArray(ByteArray))
915
import Data.Bytes.Types (ByteArrayN(ByteArrayN))
1016
import GHC.TypeNats (type (+))
17+
import Arithmetic.Types (Nat, Nat#)
18+
19+
import qualified Data.Primitive as PM
20+
import qualified Arithmetic.Unsafe as Unsafe
21+
import qualified GHC.Exts as Exts
1122

1223
append :: ByteArrayN m -> ByteArrayN n -> ByteArrayN (m + n)
1324
{-# inline append #-}
1425
append (ByteArrayN x) (ByteArrayN y) = ByteArrayN (x <> y)
26+
27+
-- | Recover a witness of the length.
28+
length :: ByteArrayN n -> Nat n
29+
{-# inline length #-}
30+
length (ByteArrayN x) = Unsafe.Nat (PM.sizeofByteArray x)
31+
32+
-- | Recover an unboxed witness of the length.
33+
length# :: ByteArrayN n -> Nat# n
34+
{-# inline length# #-}
35+
length# (ByteArrayN (ByteArray x)) = Unsafe.Nat# (Exts.sizeofByteArray# x)

0 commit comments

Comments
 (0)