| Safe Haskell | Safe-Inferred |
|---|---|
| Language | GHC2021 |
HsBindgen.Runtime.ConstantArray
Description
C arrays of known, constant size
This module is intended to be imported qualified.
import HsBindgen.Runtime.Prelude import HsBindgen.Runtime.ConstantArray qualified as CA
Synopsis
- data ConstantArray (n :: Nat) a
- toVector :: forall a n arrayLike. Coercible arrayLike (ConstantArray n a) => arrayLike -> (Proxy n, Vector a)
- fromVector :: forall a n arrayLike. (Coercible arrayLike (ConstantArray n a), Storable a, KnownNat n, HasCallStack) => Proxy n -> Vector a -> arrayLike
- toPtr :: forall arrayLike n a. Coercible arrayLike (ConstantArray n a) => Proxy n -> Ptr a -> Ptr arrayLike
- toFirstElemPtr :: forall arrayLike n a. Coercible arrayLike (ConstantArray n a) => Ptr arrayLike -> (Proxy n, Ptr a)
- withPtr :: forall b n a r. (Coercible b (ConstantArray n a), Storable a) => b -> (Ptr b -> IO r) -> IO r
- repeat :: forall n a. (KnownNat n, Storable a) => a -> ConstantArray n a
- fromList :: forall n a. (KnownNat n, Storable a, HasCallStack) => [a] -> ConstantArray n a
- toList :: Storable a => ConstantArray n a -> [a]
- intVal :: forall n. KnownNat n => Proxy n -> Int
Documentation
data ConstantArray (n :: Nat) a Source #
A C array of known size
Instances
toVector :: forall a n arrayLike. Coercible arrayLike (ConstantArray n a) => arrayLike -> (Proxy n, Vector a) Source #
fromVector :: forall a n arrayLike. (Coercible arrayLike (ConstantArray n a), Storable a, KnownNat n, HasCallStack) => Proxy n -> Vector a -> arrayLike Source #
Pointers
In example C code below, p1 points to the array xs as a whole, while p2
points to the first element of xs.
extern int xs[3];
void foo () {
int (*p1)[3] = &xs;
int *p2 = &(xs[0]);
}Though the types of p1 and p2 differ, the values of the pointers (the
address they point to) is the same. An array is just a block of contiguous
memory storing array elements. p1 points to where xs starts, and p2
points to where the first element of xs starts, and these addresses are the
same. In Haskell, the corresponding types for p1 and p2 respectively are
and Ptr (ConstantArray n CInt) respectively.Ptr CInt
Functions like peek require a argument. If
the user only has access to a Ptr (ConstantArray n a) but they know that is pointing to the
first element in an array, then they can use Ptr atoPtr to convert the
pointer before using peekArray on it. Conversely, if the user has access to
a but they want to convert it to a Ptr (ConstantArray n a),
then they can use Ptr a.toFirstElemPtr
NOTE: with overloaded record dot syntax, syntax like .toFirstElemPtr is
also supported.
Relevant functions in this module also support pointers of newtypes around
ConstantArray, hence the addition of Coercible constraints in many
places. For example, we can use toPtr at a ConstantArray type
or we can use toPtr at a newtype around a ConstantArray.
newtype A n = A (ConstantArray n CInt) toPtr @(ConstantArray 3 CInt) :: Proxy 3 -> Ptr CInt -> Ptr (ConstantArray 3 CInt) toPtr @(A 3) :: Proxy 3 -> Ptr CInt -> Ptr (A 3)
toPtr :: forall arrayLike n a. Coercible arrayLike (ConstantArray n a) => Proxy n -> Ptr a -> Ptr arrayLike Source #
( O(1) ): Use a pointer to the first element of an array as a pointer to the whole of said array.
NOTE: this function does not check that the pointer is actually a pointer to the first element of an array.
toFirstElemPtr :: forall arrayLike n a. Coercible arrayLike (ConstantArray n a) => Ptr arrayLike -> (Proxy n, Ptr a) Source #
( O(1) ): Use a pointer to a whole array as a pointer to the first element of said array.
withPtr :: forall b n a r. (Coercible b (ConstantArray n a), Storable a) => b -> (Ptr b -> IO r) -> IO r Source #
( O(n) ): Retrieve the underlying pointer
Construction
fromList :: forall n a. (KnownNat n, Storable a, HasCallStack) => [a] -> ConstantArray n a Source #
( O(n) ): Construct from a list
Precondition: the list must have the right number of elements.
Query
toList :: Storable a => ConstantArray n a -> [a] Source #
( O(n) )