pub unsafe trait Recyclable: Sized {
// Required method
unsafe fn recycle(ptr: NonNull<Self>);
// Provided methods
fn allocate(_value: Self) -> Result<NonNull<Self>, AllocError> { ... }
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, if overridden, returns a valid pointer that can be safely deallocated byrecycle.recyclecorrectly deallocates the pointer and does not cause double-free or use-after-free.
Required Methods§
Provided Methods§
Sourcefn allocate(_value: Self) -> Result<NonNull<Self>, AllocError>
fn allocate(_value: Self) -> Result<NonNull<Self>, AllocError>
Allocates a new instance of Self.
The default implementation returns Err(AllocError), which is appropriate for types
that cannot be allocated from Rust (e.g., C++ ref-counted objects).
Sourceunsafe fn recycle_ffi(ptr: *mut c_void)
unsafe fn recycle_ffi(ptr: *mut c_void)
Helper for FFI functions to call recycle.
§Safety
- The caller must ensure that
ptris valid and points to a valid object of typeSelfthat 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".