pub struct VTableDirect {
pub display: Option<unsafe fn(*const (), &mut Formatter<'_>) -> Result<(), Error>>,
pub debug: Option<unsafe fn(*const (), &mut Formatter<'_>) -> Result<(), Error>>,
pub hash: Option<unsafe fn(*const (), &mut HashProxy<'_>)>,
pub invariants: Option<unsafe fn(*const ()) -> Result<(), String>>,
pub parse: Option<unsafe fn(&str, *mut ()) -> Result<(), ParseError>>,
pub parse_bytes: Option<unsafe fn(&[u8], *mut ()) -> Result<(), ParseError>>,
pub try_from: Option<unsafe fn(*mut (), &'static Shape, PtrConst) -> TryFromOutcome>,
pub try_into_inner: Option<unsafe fn(*mut ()) -> Result<PtrMut, String>>,
pub try_borrow_inner: Option<unsafe fn(*const ()) -> Result<PtrMut, String>>,
pub partial_eq: Option<unsafe fn(*const (), *const ()) -> bool>,
pub partial_cmp: Option<unsafe fn(*const (), *const ()) -> Option<Ordering>>,
pub cmp: Option<unsafe fn(*const (), *const ()) -> Ordering>,
}Expand description
VTable for concrete types with compile-time known traits.
Uses thin pointers (*const (), *mut ()) as receivers.
Used for scalars, String, user-defined structs/enums, etc.
§Per-type operations
Note that drop_in_place, default_in_place, and clone_into are NOT in this struct.
These operations must be monomorphized per-type and live in TypeOps on the crate::Shape.
This allows vtables to be shared across generic instantiations (e.g., one vtable for all Vec<T>).
§Safety
All function pointers are unsafe fn because they operate on raw pointers.
Callers must ensure:
- The pointer points to a valid instance of the expected type
- The pointer has the correct alignment for the type
- For mutable operations, the caller has exclusive access to the data
- The lifetime of the data extends for the duration of the operation
Fields§
§display: Option<unsafe fn(*const (), &mut Formatter<'_>) -> Result<(), Error>>Display function - formats value using Display trait.
debug: Option<unsafe fn(*const (), &mut Formatter<'_>) -> Result<(), Error>>Debug function - formats value using Debug trait.
hash: Option<unsafe fn(*const (), &mut HashProxy<'_>)>Hash function - hashes value using Hash trait via HashProxy.
invariants: Option<unsafe fn(*const ()) -> Result<(), String>>Invariants function - checks type invariants.
parse: Option<unsafe fn(&str, *mut ()) -> Result<(), ParseError>>Parse function - parses value from string into destination.
parse_bytes: Option<unsafe fn(&[u8], *mut ()) -> Result<(), ParseError>>Parse bytes function - parses value from byte slice into destination. Used for binary formats where types have a more efficient representation.
try_from: Option<unsafe fn(*mut (), &'static Shape, PtrConst) -> TryFromOutcome>Try from function - converts from another value type.
§Arguments
dst: Destination pointer where the converted value will be writtensrc_shape: Shape of the source typesrc_ptr: Pointer to the source value
§Return Value
Returns crate::TryFromOutcome which encodes both the result and ownership semantics:
crate::TryFromOutcome::Converted: Success. Source was consumed.crate::TryFromOutcome::Unsupported: Source type not supported. Source was NOT consumed.crate::TryFromOutcome::Failed: Conversion failed. Source WAS consumed.
This design allows callers to attempt multiple try_from conversions in sequence
until one succeeds, without losing the source value on type mismatches.
§Implementation Guidelines
Implementations should follow this pattern:
// Check type BEFORE consuming - only consume supported types
if src_shape.id == <String as Facet>::SHAPE.id {
let value = src.read::<String>(); // Consume the value
match convert(value) {
Ok(converted) => {
unsafe { dst.write(converted) };
TryFromOutcome::Converted
}
Err(e) => TryFromOutcome::Failed(e.into()),
}
} else if src_shape.id == <&str as Facet>::SHAPE.id {
// Copy types can use get() since they're trivially copyable
let value: &str = *src.get::<&str>();
match convert_str(value) {
Ok(converted) => {
unsafe { dst.write(converted) };
TryFromOutcome::Converted
}
Err(e) => TryFromOutcome::Failed(e.into()),
}
} else {
// Unsupported type - return WITHOUT consuming
TryFromOutcome::Unsupported
}§Safety
dstmust be valid for writes and properly aligned for the destination typesrc_ptrmust point to valid, initialized memory of the type described bysrc_shape
try_into_inner: Option<unsafe fn(*mut ()) -> Result<PtrMut, String>>Try into inner function - extracts inner value (consuming).
try_borrow_inner: Option<unsafe fn(*const ()) -> Result<PtrMut, String>>Try borrow inner function - borrows inner value.
partial_eq: Option<unsafe fn(*const (), *const ()) -> bool>PartialEq function - tests equality with another value.
partial_cmp: Option<unsafe fn(*const (), *const ()) -> Option<Ordering>>PartialOrd function - compares with another value.
cmp: Option<unsafe fn(*const (), *const ()) -> Ordering>Ord function - total ordering comparison.
Implementations§
Source§impl VTableDirect
impl VTableDirect
Sourcepub const fn empty() -> VTableDirect
pub const fn empty() -> VTableDirect
Create an empty VTableDirect with all fields set to None.
Sourcepub const fn builder() -> VTableDirect
pub const fn builder() -> VTableDirect
Start building a new VTableDirect (untyped).
Source§impl VTableDirect
impl VTableDirect
Sourcepub const fn builder_for<T>() -> TypedVTableDirectBuilder<T>
pub const fn builder_for<T>() -> TypedVTableDirectBuilder<T>
Create a typed builder for type T.
The builder ensures all function pointers are for the same type T.
Trait Implementations§
Source§impl Clone for VTableDirect
impl Clone for VTableDirect
Source§fn clone(&self) -> VTableDirect
fn clone(&self) -> VTableDirect
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more