pub trait Compact: Sized + Clone {
// Required methods
fn is_still_compact(&self) -> bool;
fn dynamic_size_bytes(&self) -> usize;
unsafe fn compact(
source: *mut Self,
dest: *mut Self,
new_dynamic_part: *mut u8,
);
unsafe fn decompact(source: *const Self) -> Self;
// Provided methods
fn total_size_bytes(&self) -> usize { ... }
unsafe fn behind(ptr: *mut Self) -> *mut u8 { ... }
unsafe fn compact_behind(source: *mut Self, dest: *mut Self) { ... }
}
Expand description
A trait for objects with a statically-sized part and a potential dynamically-sized part that can be stored both compactly in consecutive memory or freely on the heap
Required Methods§
Sourcefn is_still_compact(&self) -> bool
fn is_still_compact(&self) -> bool
Is the object’s dynamic part stored compactly?
Sourcefn dynamic_size_bytes(&self) -> usize
fn dynamic_size_bytes(&self) -> usize
Size of the dynamic part in bytes
Sourceunsafe fn compact(source: *mut Self, dest: *mut Self, new_dynamic_part: *mut u8)
unsafe fn compact(source: *mut Self, dest: *mut Self, new_dynamic_part: *mut u8)
Copy the static part of source
to dest
and compactly store
the dynamic part of source
as the new dynamic part of dest
at new_dynamic_part
.
This semantically moves source into dest.
Sourceunsafe fn decompact(source: *const Self) -> Self
unsafe fn decompact(source: *const Self) -> Self
Creates a clone of self with the dynamic part guaranteed to be stored freely.
Note: if the dynamic part was already stored freely, the calling environment has to make sure that old self will not be dropped, as this might lead to a double free!
This is mostly used internally to correctly implement
Compact
datastructures that contain Compact
elements.
Provided Methods§
Sourcefn total_size_bytes(&self) -> usize
fn total_size_bytes(&self) -> usize
Total size of the object (static part + dynamic part)
Sourceunsafe fn behind(ptr: *mut Self) -> *mut u8
unsafe fn behind(ptr: *mut Self) -> *mut u8
Get a pointer to behind the static part of self
(commonly used place for the dynamic part)
Sourceunsafe fn compact_behind(source: *mut Self, dest: *mut Self)
unsafe fn compact_behind(source: *mut Self, dest: *mut Self)
Like compact
with new_dynamic_part
set to dest.behind()
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§
impl Compact for CompactString
impl<K: Copy + Eq + Hash, V: Compact, A: Allocator> Compact for OpenAddressingMap<K, V, A>
impl<K: Copy, V: Compact + Clone, A: Allocator> Compact for CompactDict<K, V, A>
impl<T: Clone + Compact> Compact for CompactOption<T>
impl<T: Copy> Compact for T
Trivial implementation for fixed-sized, Copy
types (no dynamic part)