Pointer

public struct Pointer[T] where T: not Copyable, T: not Static { /* private fields */ }

Properties

public var address: UInt64 { get }

Live view of the value at the address. get reads through the pointer; set writes. Both are unchecked — see # Safety.

Safety

The pointer must be non-null and the storage must hold a valid initialised T. Reading past the end of an allocation, after the pointee has been freed, or through a dangling pointer is undefined behavior. Numeric address — same value as asRaw().address.

public var isNull: Bool { get }

Convenience for address == 0.

public var mutatingValue: &mutating T { get }

Mutable borrowed view of the pointee. Mutating methods and mutating-convention arguments through the result write the storage in place — no clone, no write-back.

Safety

  • Same validity preconditions as value.
  • This is the const-cast escape hatch: Pointer(to: x).mutatingValue on a let/shared place compiles and writes through it — the compiler does not stop it (same class as write() after Pointer(to:) on a let).
  • Writing through a pointer into storage shared by a COW container (Array, String, Dictionary) is visible through every copy, breaking value semantics. Make the storage unique first.
public var pointee: T { get set }
public var raw: lang.ptr[T] { get }

The wrapped primitive pointer.

public var value: &T { get }

Borrowed view of the pointee — no copy, no clone, no T: Copyable requirement. Member access, operators, and borrow-convention calls go through it in place; binding it (let x = p.value;) stores an owned copy instead (binding decay).

Safety

  • The pointer must be non-null and the storage must hold a valid, initialized T for as long as the reference is used.
  • The reference inherits this pointer's contract: the compiler does not verify the pointee's lifetime. Using it after the storage is freed is undefined behavior — the same trust point as read(), returning a view instead of a copy.

Associated Types

type Target = T

Initializers

public init(raw: lang.ptr[T])

Wraps an existing primitive pointer.

public init(to: T)

Takes the address of value. Equivalent to &value in C — the borrowed place itself is captured; no copy is made.

Safety

The pointer does not keep value alive: the caller must ensure the place outlives every use of the resulting pointer, or any read is undefined behavior.

This is the sole capture init and it accepts any place — var or let — yielding the same write-capable Pointer[T]. Writing through a pointer captured from an immutable place is the C const-cast footgun: it compiles, and it is on the caller to know the storage is actually mutable.

Methods

public func asRaw() -> RawPointer

Drops the type tag, returning a RawPointer to the same address.

public func cast[U]() -> Pointer[U] where U: not Copyable

Reinterprets the address as a Pointer[U].

Safety

Same caveats as RawPointer.cast — the storage must be valid for U (size, alignment, contents) at the moment of the read/write.

public func dropInPlace()

Runs T's destructor at this address without copying the value to stack. The pointer remains valid but the pointee is left in a destroyed state.

public static func nullPointer() -> Pointer[T]

Returns a typed null pointer.

public func offset(by: Int64) -> Pointer[T]

Strides the pointer by n elements (multiplied by sizeof[T]). Compare with RawPointer.offset, which strides by raw bytes.

public func pointeeRef() -> &T
public func read() -> T where T: Copyable

Bit-copies T out of the address — T.deinit does not run. This is a copy, so it requires T: Copyable; for non-Copyable pointees use with (borrow) or move (consuming read-out).

public func take() -> T

Moves T out of the address bitwise — a consuming read-out. Unlike read() there is no Copyable requirement: ownership of the value transfers to the caller, and the pointee is left logically uninitialised. T.deinit does not run here.

This is the dual of write(consuming:) and the building block for relocating non-Copyable values (e.g. swap).

Safety

Same validity preconditions as read(), and additionally: after the take the pointee must not be read or dropped until the slot is re-initialised (e.g. via write). Taking the same slot twice without an intervening write double-owns the value and will double-free.

public func with[R]((T) -> R) -> R

Borrows the pointee in place and passes it to body. The pointee is never copied or cloned — T.deinit does not run. Use this to extract fields from heap-allocated structs without triggering resource cleanup on a temporary clone.

public func withMut[R]((T) -> R) -> R

Mutably borrows the pointee in place and passes it to body as a mutating argument. The pointee is mutated directly on the heap — never copied, cloned, or written back — so T.deinit does not run on a temporary. This is the in-place primitive behind COW modify.

public func write(consuming T)

Writes value through the pointer. Same safety preconditions as pointee.set.

Safety

The pointer must be non-null and the storage valid for writes of T. The previous pointee is overwritten without running its deinit. If the pointer was captured with Pointer(to:) from a let place, writing is the documented const-cast footgun — the compiler does not stop it.

ImplementsEquatable

Associated Types

type Output = Bool

Methods

public func equal(to: Self) -> Bool

Bridges Equal.equal(to:) to Equatable.isEqual(to:).

public func isEqual(to: Pointer[T]) -> Bool

Address-based equality.

public func notEqual(to: Self) -> Bool

Default !=: delegates to == so there's a single source of truth.

ImplementsHashable

Methods

public func hash[H](into: mutating H) where H: Hasher

Hashes the underlying address.

Heap allocations cluster on alignment boundaries, so the raw address has predictable low bits. We run the address through Murmur3's fmix64 finalizer (two rounds of xor-shift / multiply) before hashing so every input bit avalanches across the 64-bit output. Without this, pointer-keyed maps see collision clustering driven by the allocator's stride.

ImplementsMutableIndirection

Methods

public mutating func pointeeMutRef() -> &mutating T

Defined in lang/std/memory/pointer.ks