hs-bindgen-runtime
Safe HaskellSafe-Inferred
LanguageGHC2021

HsBindgen.Runtime.IncompleteArray

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

Documentation

data IncompleteArray a Source #

A C array of unknown size

Instances

Instances details
HasField "toFirstElemPtr" (Ptr (IncompleteArray a)) (Ptr a) Source #

toFirstElemPtr for overloaded record dot syntax

Instance details

Defined in HsBindgen.Runtime.IncompleteArray

Methods

getField :: Ptr (IncompleteArray a) -> Ptr a #

(Show a, Storable a) => Show (IncompleteArray a) Source # 
Instance details

Defined in HsBindgen.Runtime.IncompleteArray

(Storable a, Eq a) => Eq (IncompleteArray a) Source # 
Instance details

Defined in HsBindgen.Runtime.IncompleteArray

toVector :: Coercible arrayLike (IncompleteArray a) => arrayLike -> Vector a Source #

( O(1) ): Get the underlying Vector representation

This makes the full Vector API available.

fromVector :: Coercible arrayLike (IncompleteArray a) => Vector a -> arrayLike Source #

( O(1) ): Construct from a Vector representation

This makes the full Vector API available.

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 Ptr (IncompleteArray CInt) and Ptr CInt respectively.

Functions like peekArray require a Ptr (IncompleteArray a) argument. If the user only has access to a Ptr a but they know that is pointing to the first element in an array, then they can use toPtr to convert the pointer before using peekArray on it. Conversely, if the user has access to a Ptr (IncompleteArray a) but they want to convert it to a Ptr a, then they can use 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

repeat :: Storable a => Int -> a -> IncompleteArray a Source #

( O(n) )

fromList :: Storable a => [a] -> IncompleteArray a Source #

( O(n) )

Query

toList :: Storable a => IncompleteArray a -> [a] Source #

( O(n) )