README

tonyfettes/c/pointer does not have a README file

#
Borrow

trait Borrow

Trait for types that support safe borrowing as pointers.

Types implementing this trait can be safely converted to and from pointers, enabling temporary pointer access to their internal data.
impl Borrow for FixedArray[T]
impl Borrow for Bytes
impl Borrow for Ref[T]

#
From

pub(open) trait From {
fn from(Self) -> Pointer[Unit]
}

#
Into

pub(open) trait Into {
fn into(Pointer[Unit]) -> Self
}

#
Load

pub(open) trait Load {
fn load(Pointer[Self]) -> Self
}

impl Load for Bool
impl Load for Byte
impl Load for Int
impl Load for Int16
impl Load for Int64
impl Load for UInt
impl Load for UInt16
impl Load for UInt64
impl Load for Float
impl Load for Double

#
Store

pub(open) trait Store {
fn store(Pointer[Self], Self) -> Unit
}

impl Store for Bool
impl Store for Byte
impl Store for Int
impl Store for Int16
impl Store for Int64
impl Store for UInt
impl Store for UInt16
impl Store for UInt64
impl Store for Float
impl Store for Double

#
Pointer

#alias(Ptr)
#external
pub(all) type Pointer[_]

A generic pointer type that wraps the underlying platform pointer type.

Pointer[T] represents a pointer to a value of type T. This type is used for low-level memory operations and interoperability with C code.
impl Compare for Pointer[T]
impl Eq for Pointer[T]
impl Borrow for Pointer[T]
impl From for Pointer[T]
impl Into for Pointer[T]
impl Load for Pointer[T]
impl Store for Pointer[T]

#
Pointer::add

Adds an offset to a pointer and returns a new pointer.

Parameters:

  • pointer: The original pointer.
  • offset: The offset to add, in number of elements of type T.

Returns a new pointer that is offset from the original pointer by the specified number of elements.

#
Pointer::align

fn Pointer::align(self : Pointer[Unit], alignment :
Size
) -> Pointer[Unit]

#
Pointer::byte_add

fn[T] Pointer::byte_add(self : Pointer[T], offset :
Size
) -> Pointer[T]

#
Pointer::byte_offset

fn[T] Pointer::byte_offset(self : Pointer[T], offset :
Ptrdiff
) -> Pointer[T]

Adds a byte offset to a pointer and returns a new pointer.

This function interprets the pointer as a byte pointer, adds the specified byte offset, and returns the result cast back to the original type.

Parameters:
  • offset: The byte offset to add

Returns a new pointer that is offset from the original pointer by the specified number of bytes.

#
Pointer::byte_offset_from

fn[T] Pointer::byte_offset_from(self : Pointer[T], other : Pointer[T]) ->
Ptrdiff

Calculates the byte offset between two pointers.

Subtracts other from self to determine how many bytes are between the two pointers.

Parameters:
  • other: The pointer to subtract from this pointer

Returns the number of bytes between the two pointers.

#
Pointer::byte_sub

fn[T] Pointer::byte_sub(self : Pointer[T], offset :
Size
) -> Pointer[T]

#
Pointer::cast

fn[T, U] Pointer::cast(self : Pointer[T]) -> Pointer[U]

Casts a pointer from one type to another.

This is an unsafe operation that reinterprets the pointer without any type checking.

Parameters:
  • self: The pointer to cast

Returns a pointer to type U

#
Pointer::cast_to_byte

fn[T] Pointer::cast_to_byte(self : Pointer[T]) -> Pointer[Byte]

#
Pointer::cast_to_unit

fn[T] Pointer::cast_to_unit(self : Pointer[T]) -> Pointer[Unit]

#
Pointer::from

#as_free_fn
fn[T : From] Pointer::from(value : T) -> Pointer[Unit]

#
Pointer::into

fn[T : Into] Pointer::into(ptr : Pointer[Unit]) -> T

#
Pointer::is_not_null

fn[T] Pointer::is_not_null(self : Pointer[T]) -> Bool

Checks if the pointer is not null.

Returns true if the pointer is not null, false otherwise.

#
Pointer::is_null

fn[T] Pointer::is_null(self : Pointer[T]) -> Bool

Checks if the pointer is null.

Returns true if the pointer is null, false otherwise.

#
Pointer::load

fn[T : Load] Pointer::load(self : Pointer[T]) -> T

Loads a value from the memory location pointed to by this pointer.

This is equivalent to dereferencing the pointer in C (*pointer).

Returns the value stored at this memory location.

#
Pointer::null

#as_free_fn
fn[T] Pointer::null() -> Pointer[T]

Creates a null pointer of type T.

Returns a null pointer that points to no valid memory location.

#
Pointer::offset

#
Pointer::offset_from

Calculates the offset between two pointers in elements.

Subtracts other from self to determine how many elements of type T are between the two pointers.

Parameters:
  • other: The pointer to subtract from this pointer

Returns the number of elements between the two pointers.

#
Pointer::op_get

fn[T :
Sized
+ Load] Pointer::op_get(self : Pointer[T], index : Int) -> T

Reads a value from the pointer at the specified index.

This is equivalent to the C operation pointer[index].

Parameters:
  • index: The index to read from

Returns the value at the specified index.

#
Pointer::op_set

fn[T :
Sized
+ Store] Pointer::op_set(self : Pointer[T], index : Int, value : T) -> Unit

Writes a value to the pointer at the specified index.

This is equivalent to the C operation pointer[index] = value.

Parameters:
  • index: The index to write to
  • value: The value to write

#
Pointer::sizeof

#as_free_fn
fn Pointer::sizeof() ->
Size

Returns the size of a pointer in bytes.

On most platforms, this returns 8 for 64-bit systems or 4 for 32-bit systems.

#
Pointer::store

fn[T : Store] Pointer::store(self : Pointer[T], value : T) -> Unit

Stores a value to the memory location pointed to by this pointer.

This is equivalent to the C operation *pointer = value.

Parameters:
  • value: The value to store

#
Pointer::to_size

fn[T] Pointer::to_size(self : Pointer[T]) ->
Size

#
Pointer::to_uint64

fn[T] Pointer::to_uint64(self : Pointer[T]) -> UInt64

Converts the pointer to a 64-bit unsigned integer.

This returns the numeric value of the pointer's address.

Returns the memory address as a UInt64.

#
Pointer::unsafe_from

#as_free_fn
fn[T] Pointer::unsafe_from(value : T) -> Pointer[Unit]

Unsafely extracts a pointer from any value.

Warning: This is an unsafe operation that should be used with extreme caution. The value is not consumed, and using the pointer may lead to undefined behavior if the value is moved or deallocated.

Parameters:

  • value: The value to extract a pointer from

Returns a Pointer[Unit] pointing to the value's memory location.

#
Pointer::unsafe_from_array

#as_free_fn
fn[T] Pointer::unsafe_from_array(value : FixedArray[T]) -> Pointer[T]

Extracts a pointer from a fixed array.

Parameters:

  • value: The fixed array to extract a pointer from

Returns a Pointer[T] pointing to the first element of the array.

#
Pointer::unsafe_from_bytes

#as_free_fn
fn Pointer::unsafe_from_bytes(value : Bytes) -> Pointer[Byte]

Extracts a pointer from a bytes buffer.

Parameters:

  • value: The bytes buffer to extract a pointer from

Returns a Pointer[Byte] pointing to the first byte of the buffer.

#
Pointer::unsafe_from_ref

#as_free_fn
fn[T] Pointer::unsafe_from_ref(value :
Ref
[T]) -> Pointer[T]

Extracts a pointer from a reference.

Parameters:

  • value: The reference to extract a pointer from

Returns a Pointer[T] pointing to the referenced value.

#
Pointer::unsafe_into

#as_free_fn
fn[T] Pointer::unsafe_into(ptr : Pointer[Unit]) -> T

Unsafely converts a pointer back into a value.

Warning: This is an unsafe operation that should be used with extreme caution. The pointer must point to valid memory of type T, or undefined behavior will occur.

Parameters:

  • pointer: The pointer to convert

Returns a value of type T.

#
Pointer::unsafe_into_array

#as_free_fn
fn[T] Pointer::unsafe_into_array(ptr : Pointer[T]) -> FixedArray[T]

Converts a pointer into a fixed array.

Parameters:

  • pointer: The pointer to convert

Returns a FixedArray[T] starting at the pointer's memory location.

#
Pointer::unsafe_into_bytes

#as_free_fn
fn Pointer::unsafe_into_bytes(ptr : Pointer[Byte]) -> Bytes

Converts a pointer into a bytes buffer.

Parameters:

  • pointer: The pointer to convert

Returns a Bytes buffer starting at the pointer's memory location.

#
Pointer::unsafe_into_ref

#as_free_fn
fn[T] Pointer::unsafe_into_ref(ptr : Pointer[T]) ->
Ref
[T]

Converts a pointer into a reference.

Parameters:

  • pointer: The pointer to convert

Returns a Ref[T] referencing the memory location.

#
borrow

fn[T : Borrow, R] borrow(value : T, f : (Pointer[Unit]) -> R raise?) -> R raise?

Borrows a pointer from a value that implements the Borrow trait.

The value is temporarily converted to a pointer, passed to the function, and then restored. This ensures the value remains valid after the operation.

Parameters:

  • value: The value to borrow from (must implement Borrow)
  • f: The function to call with the borrowed pointer

Returns the result of calling f.

#
borrow_2

fn[A : Borrow, B : Borrow, R] borrow_2(a : A, b : B, f : (Pointer[Unit], Pointer[Unit]) -> R raise?) -> R raise?

#
borrow_array

fn[T, R] borrow_array(array : FixedArray[T], f : (Pointer[T]) -> R raise?) -> R raise?

Borrows a pointer from a fixed array for the duration of a function call.

The array is temporarily converted to a pointer, passed to the function, and then restored. This ensures the array remains valid after the operation.

Parameters:

  • array: The fixed array to borrow from
  • f: The function to call with the borrowed pointer

Returns the result of calling f.

#
borrow_bytes

fn[R] borrow_bytes(bytes : Bytes, f : (Pointer[Byte]) -> R raise?) -> R raise?

Borrows a pointer from a bytes buffer for the duration of a function call.

The bytes buffer is temporarily converted to a pointer, passed to the function, and then restored. This ensures the buffer remains valid after the operation.

Parameters:

  • bytes: The bytes buffer to borrow from
  • f: The function to call with the borrowed pointer

Returns the result of calling f.

#
borrow_ref

fn[T, R] borrow_ref(ref_ :
Ref
[T], f : (Pointer[T]) -> R raise?) -> R raise?

Borrows a pointer from a reference for the duration of a function call.

The reference is temporarily converted to a pointer, passed to the function, and then restored. This ensures the reference remains valid after the operation.

Parameters:

  • ref_: The reference to borrow from
  • f: The function to call with the borrowed pointer

Returns the result of calling f.

#
unsafe_borrow

fn[T, R] unsafe_borrow(value : T, f : (Pointer[Unit]) -> R raise?) -> R raise?

Unsafely borrows a pointer from any value for the duration of a function call.

Warning: This is an unsafe operation. The value is temporarily converted to a pointer and restored after the function call, but this may lead to undefined behavior if not used carefully.

Parameters:

  • value: The value to borrow from
  • f: The function to call with the borrowed pointer

Returns the result of calling f.

#
unsafe_decref

fn[T] unsafe_decref(value : T) -> Unit

Decrements the reference count of a value.

This function manipulates the internal reference count by converting the value to a pointer and back twice, effectively decrementing its reference count.

Warning: This is a low-level operation that should be used with caution.

Parameters:

  • value: The value whose reference count should be decremented