Skip to main content

Recyclable

Trait Recyclable 

Source
pub unsafe trait Recyclable: Sized {
    // Required methods
    fn allocate(value: Self) -> Result<NonNull<Self>, AllocError>;
    unsafe fn recycle(ptr: NonNull<Self>);

    // Provided method
    unsafe fn recycle_ffi(ptr: *mut c_void) { ... }
}
Expand description

Trait for types that can be recycled (deallocated).

§Safety

Implementing this trait is unsafe because the implementer must ensure that:

  • allocate returns a valid pointer that can be safely deallocated by recycle.
  • recycle correctly deallocates the pointer and does not cause double-free or use-after-free.

Required Methods§

Source

fn allocate(value: Self) -> Result<NonNull<Self>, AllocError>

Allocates a new instance of Self.

Source

unsafe fn recycle(ptr: NonNull<Self>)

Recycles the object.

§Safety
  • The caller must ensure that ptr points to a valid, fully-initialized instance of Self that has no other references.
  • The caller must not use the pointer or any references derived from it after this call.

Provided Methods§

Source

unsafe fn recycle_ffi(ptr: *mut c_void)

Helper for FFI functions to call recycle.

§Safety
  • The caller must ensure that ptr is valid and points to a valid object of type Self that has no other references.
  • The caller must not use the pointer or any references derived from it after this call.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§