Trait bones_schema::prelude::HasSchema
source · pub unsafe trait HasSchema: Sync + Send + 'static {
// Required method
fn schema() -> &'static Schema;
// Provided methods
fn register_schema() { ... }
fn cast<T: HasSchema>(this: &Self) -> &T { ... }
fn try_cast<T: HasSchema>(this: &Self) -> Result<&T, SchemaMismatchError> { ... }
fn cast_mut<T: HasSchema>(this: &mut Self) -> &mut T { ... }
fn try_cast_mut<T: HasSchema>(
this: &mut Self,
) -> Result<&mut T, SchemaMismatchError> { ... }
fn as_schema_ref(&self) -> SchemaRef<'_>
where Self: Sized { ... }
fn as_schema_mut(&mut self) -> SchemaRefMut<'_>
where Self: Sized { ... }
}
Expand description
Trait implemented for types that have a Schema
.
§Safety
This trait is unsafe to implement manually because it makes claims about the memory layout of a
type that may be depended on in unsafe code, but it is safe to derive HasSchema
on supported
types.
If implemented manually, you must ensure that the schema accurately describes the memory layout of the type, or else accessing the type according to the schema would be unsound.
Required Methods§
Provided Methods§
sourcefn register_schema()
fn register_schema()
Register this schema with the global schema registry.
This is automatically done by the framework in many cases, whenever
schema()
is called, but it may be necessary sometimes to
manually register it.
sourcefn cast<T: HasSchema>(this: &Self) -> &T
fn cast<T: HasSchema>(this: &Self) -> &T
Cast a reference of this type to a reference of another type with the same memory layout.
§Panics
Panics if the schema of T
doesn’t match the schema of Self
.
sourcefn try_cast<T: HasSchema>(this: &Self) -> Result<&T, SchemaMismatchError>
fn try_cast<T: HasSchema>(this: &Self) -> Result<&T, SchemaMismatchError>
Cast a reference of this type to a reference of another type with the same memory layout.
§Errors
Errors if the schema of T
doesn’t match the schema of Self
.
sourcefn cast_mut<T: HasSchema>(this: &mut Self) -> &mut T
fn cast_mut<T: HasSchema>(this: &mut Self) -> &mut T
Cast a mutable reference of this type to a reference of another type with the same memory layout.
§Panics
Panics if the schema of T
doesn’t match the schema of Self
.
sourcefn try_cast_mut<T: HasSchema>(
this: &mut Self,
) -> Result<&mut T, SchemaMismatchError>
fn try_cast_mut<T: HasSchema>( this: &mut Self, ) -> Result<&mut T, SchemaMismatchError>
Cast a mutable reference of this type to a reference of another type with the same memory layout.
§Errors
Errors if the schema of T
doesn’t match the schema of Self
.
sourcefn as_schema_ref(&self) -> SchemaRef<'_>where
Self: Sized,
fn as_schema_ref(&self) -> SchemaRef<'_>where
Self: Sized,
Converts a reference of T
to a SchemaRef
sourcefn as_schema_mut(&mut self) -> SchemaRefMut<'_>where
Self: Sized,
fn as_schema_mut(&mut self) -> SchemaRefMut<'_>where
Self: Sized,
Converts a reference of T
to a SchemaRefMut