Expand description
The ptr module contains smart pointer types that are used throughout Bun.
Per PORTING.md §Pointers, most consumers of bun.ptr.* map directly to std
types (Box, Rc, Arc, Cow) and bun_collections (TaggedPtr,
TaggedPtrUnion). This crate hosts the intrusive/FFI-crossing variants.
Re-exports§
pub use tagged_pointer::TaggedPtr as TaggedPointer;pub use tagged_pointer::TaggedPtrUnion as TaggedPointerUnion;pub use ref_count::AnyRefCounted;pub use ref_count::CellRefCounted;pub use ref_count::RefCount;pub use ref_count::RefCounted;pub use ref_count::RefPtr;pub use ref_count::ScopedRef;pub use ref_count::ThreadSafeRefCount;pub use ref_count::ThreadSafeRefCounted;pub use ref_count::finalize_js_box;pub use ref_count::finalize_js_box_noop;pub use parent_ref::Anchored;pub use parent_ref::LiveMarker;pub use parent_ref::ParentRef;pub use raw_ref_count::RawRefCount;pub use weak_ptr::WeakPtr;pub use detach_lifetime_ref as detach_ref;
Modules§
- cow_
slice - “Copy on write” slice. There are many instances when it is desired to re-use
a slice, but doing so would make it unknown if that slice should be freed.
This structure, in release builds, is the same size as
&[T], but stores one bit for if deinitialization should free the underlying memory. - external_
shared - meta
- Private utilities used in smart pointer implementations.
- owned
- Owned pointer abstractions.
- parent_
ref ParentRef<T>— non-owning back-pointer from a child/task to its mortal owner, with debug-only liveness checking.- raw_
ref_ count - A simple wrapper around an integer reference count. This type doesn’t do any memory management itself.
- ref_
count - Intrusive reference counting (single-threaded and thread-safe) +
RefPtr<T>debug-tracking wrapper. - shared
- Shared (reference-counted) pointers.
- tagged_
pointer - weak_
ptr
Macros§
- assert_
not_ freeze - Compile-time
assert!($Child: !Freeze), for code that reaches a parent through a pointer derived from&$Child(seecontainer_of). rustc passes a&Tto aFreezeTasnoalias readonly, so any store through such a pointer is deleted in release builds; this turns “someone removed the lastCellfrom$Child” into a build error at the site that depends on it instead of a heap corruption. - from_
field_ ptr from_field_ptr!(Parent, field, ptr)→*mut Parent.- impl_
field_ parent - Stamp container-of-style back-reference accessors on a child type that
is only ever constructed as the
$fieldfield of$Parent(a port of Zig’s@fieldParentPtr). - impl_
tagged_ ptr_ union - Generates
TypeListfor($($T,)*)andUnionMember<($($T,)*)>for each$T, assigning tags1024 - ito match Zig’sTagTypeEnumWithTypeMap. - intrusive_
field - Stamp
unsafe impl IntrusiveField<$F> for $T { const OFFSET = offset_of!($T, $field); }.
Structs§
- BackRef
- Non-owning, non-null back-reference to an object that outlives
self. - External
Shared - A shared pointer whose reference count is managed externally; e.g., by extern functions.
- Interned
- A byte slice backed by process-lifetime storage.
- RawSlice
- Non-owning borrowed slice whose backing storage outlives the holder.
- ThisPtr
- Non-owning,
Copyself-pointer for uSockets / FFI callback dispatch.
Enums§
- Cow
- A clone-on-write smart pointer.
Traits§
- AsCtx
Ptr &self→*mut Selfwith shared provenance, for C-callback / scopeguard ctx slots. See module-level comment above for the safety contract.- External
Shared Descriptor - Protocol for types whose reference count is managed externally (e.g., by extern functions).
- Intrusive
Field - Declares that
Selfembeds exactly one intrusiveFfield at byteOFFSET. This is the single Rust analogue of Zig’s@fieldParentPtrbuiltin: every per-moduleconst X_OFFSET: usizetrait the port grew (TASK_OFFSET,MIXIN_OFFSET,CHANNEL_OFFSET,LazyBool<_, const OFFSET>,from_task, …) is the same(Parent, Field, OFFSET)triple pluscontainer_ofarithmetic — this trait is exactly that triple, with both directions provided. - Laundered
Self - Marker trait for types whose
&mut selfmethods launderselfthroughcore::hint::black_box(PORT_NOTES_PLAN R-2) before dispatching a re-entrant parent/user callback, then reborrow viaLaunderedSelf::r. - Allocator
Experimental - An implementation of
Allocatorcan allocate, grow, shrink, and deallocate arbitrary blocks of data described viaLayout.
Functions§
- callback_
ctx ⚠ - Recover a typed
&mut Tfrom a C-callback’s opaque user-data pointer. - cast_
fn_ ⚠ptr - Bit-cast between fn-pointer types. Replaces Zig
@ptrCaston a function pointer when erasing only the pointee type of one or more thin-pointer parameters (e.g.extern "C" fn(*mut Ctx, …)↔extern "C" fn(*mut c_void, …)). Const-generictransmuterejects fn types; an as-cast can’t change arity. This stays one audited helper and rejects non-pointer-sizedF/Gat compile time — it does not verify thatF/Gare fn-pointer types or that their arity/ABI match (all fn pointers are pointer-sized regardless of arity); those remain caller contract. - container_
of ⚠ - Recover
*mut Pfrom a pointer to one of its fields. - container_
of_ ⚠const *const-out variant ofcontainer_of. Same safety contract.- detach_
lifetime ⚠ - Detach a slice borrow from its borrowck lifetime.
- detach_
lifetime_ ⚠mut - Detach a
&mut Tborrow from its borrowck lifetime. - detach_
lifetime_ ⚠ref - Detach a
&Tborrow from its borrowck lifetime (general?Sizedform ofdetach_lifetime).
Type Aliases§
- CowSlice
- CowSliceZ
- CowString
- Dynamic
Owned - Intrusive
Arc - Intrusive
Rc - Owned
- OwnedIn
- WTFString
- Behaves like
WTF::Ref<WTF::StringImpl>.
Derive Macros§
- Anchored
#[derive(Anchored)]— seebun_ptr::parent_refmodule docs.- Cell
RefCounted #[derive(CellRefCounted)]— see module comment above.- RefCounted
#[derive(RefCounted)]— see module comment above.- Thread
Safe RefCounted #[derive(ThreadSafeRefCounted)]— locates the embeddedThreadSafeRefCount<Self>field and emits the trait impl plus theAnyRefCountedbridge. Custom destructor via#[ref_count(destroy = …)].