| Safe Haskell | Safe-Inferred |
|---|---|
| Language | GHC2021 |
HsBindgen.Runtime.IncompleteArray
Contents
Description
C arrays of unknown size
This module is intended to be imported qualified.
import HsBindgen.Runtime.Prelude import HsBindgen.Runtime.IncompleteArray qualified as IA
Synopsis
- data IncompleteArray a
- toVector :: Coercible arrayLike (IncompleteArray a) => arrayLike -> Vector a
- fromVector :: Coercible arrayLike (IncompleteArray a) => Vector a -> arrayLike
- toPtr :: forall arrayLike a. Coercible arrayLike (IncompleteArray a) => Ptr a -> Ptr arrayLike
- toFirstElemPtr :: forall arrayLike a. Coercible arrayLike (IncompleteArray a) => Ptr arrayLike -> Ptr a
- peekArray :: forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a) => Int -> Ptr arrayLike -> IO arrayLike
- pokeArray :: forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a) => Ptr arrayLike -> arrayLike -> IO ()
- withPtr :: (Coercible b (IncompleteArray a), Storable a) => b -> (Ptr b -> IO r) -> IO r
- repeat :: Storable a => Int -> a -> IncompleteArray a
- fromList :: Storable a => [a] -> IncompleteArray a
- toList :: Storable a => IncompleteArray a -> [a]
Documentation
data IncompleteArray a Source #
A C array of unknown size
Instances
| HasField "toFirstElemPtr" (Ptr (IncompleteArray a)) (Ptr a) Source # |
|
Defined in HsBindgen.Runtime.IncompleteArray Methods getField :: Ptr (IncompleteArray a) -> Ptr a # | |
| (Show a, Storable a) => Show (IncompleteArray a) Source # | |
Defined in HsBindgen.Runtime.IncompleteArray Methods showsPrec :: Int -> IncompleteArray a -> ShowS # show :: IncompleteArray a -> String # showList :: [IncompleteArray a] -> ShowS # | |
| (Storable a, Eq a) => Eq (IncompleteArray a) Source # | |
Defined in HsBindgen.Runtime.IncompleteArray Methods (==) :: IncompleteArray a -> IncompleteArray a -> Bool # (/=) :: IncompleteArray a -> IncompleteArray a -> Bool # | |
fromVector :: Coercible arrayLike (IncompleteArray a) => 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[];
void foo () {
int (*p1)[] = &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 (IncompleteArray CInt) respectively.Ptr CInt
Functions like peekArray require a argument.
If the user only has access to a Ptr (IncompleteArray 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 (IncompleteArray 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
IncompleteArray, hence the addition of Coercible constraints in many
places. For example, we can use toPtr at an IncompleteArray type or we
can use toPtr at a newtype around an IncompleteArray.
newtype A = A (IncompleteArray CInt) toPtr @(IncompleteArray CInt) :: Ptr CInt -> Ptr (IncompleteArray CInt) toPtr @A :: Ptr CInt -> Ptr A
toPtr :: forall arrayLike a. Coercible arrayLike (IncompleteArray a) => 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 a. Coercible arrayLike (IncompleteArray a) => Ptr arrayLike -> Ptr a Source #
( O(1) ): Use a pointer to a whole array as a pointer to the first element of said array.
peekArray :: forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a) => Int -> Ptr arrayLike -> IO arrayLike Source #
( O(n) ): Peek a number of elements from a pointer to an incomplete array.
pokeArray :: forall a arrayLike. (Coercible arrayLike (IncompleteArray a), Storable a) => Ptr arrayLike -> arrayLike -> IO () Source #
( O(n) ): Poke a number of elements to a pointer to an incomplete array.
withPtr :: (Coercible b (IncompleteArray a), Storable a) => b -> (Ptr b -> IO r) -> IO r Source #
( O(n) ): Retrieve the underlying pointer
Construction
fromList :: Storable a => [a] -> IncompleteArray a Source #
( O(n) )
Query
toList :: Storable a => IncompleteArray a -> [a] Source #
( O(n) )