pub unsafe trait Dependant<'o>: Sized + 'o {
type Static: Dependant<'static>;
}Expand description
Implemented for types that use data provided by an Owner and guarantee
that internal state is protected.
§Derive implementations (recommended)
It is recommended not to implement this manually and instead use the provided proc-macro as show below.
use zc::Dependant;
#[derive(Dependant)]
pub struct MyStruct<'a> {
value: &'a str,
}§Derive implementations for Copy
If a type implements Copy it cannot support interior mutability and
therefore is a valid Dependant type.
To use a Copy type without having to implement Dependant you can tell
the derive implementation to check based on a Copy bound for a specific
field or all fields.
use zc::Dependant;
#[derive(Copy, Clone)]
pub struct CopyType;
#[derive(Dependant)]
pub struct StructWithCopy<'a> {
// This field has a `Copy` bound.
#[zc(check = "Copy")]
field_a: &'a CopyType,
// This field has the standard `Dependant` bound.
field_b: u8,
}
// All fields in this struct have the `Copy` bound.
#[derive(Dependant)]
#[zc(check = "Copy")]
pub struct StructWithAllCopy<'a> {
field_a: &'a CopyType,
field_b: u8,
}§Manual implementations
If you wish not to use the provided proc-macro you implement as shown:
struct MyStruct<'a>(&'a [u8]);
unsafe impl<'o> zc::Dependant<'o> for MyStruct<'o> {
type Static = MyStruct<'static>;
}§Safety
Implementer must guarantee:
- the structure only requires a single lifetime.
Self::Staticmust be the same type but with a'staticlifetime.
And in addition the structure:
- has no interior mutability.
OR
- can safely be stored with it’s lifetime erased (ie. as
'static). - does not provided an interface that will accept data with non-
'staticlifetime though a interior mutable interface.
§Interior Mutability
Types that provide interior mutability include both !Sync types (eg.
RefCell<T>) and Sync types (eg. Mutex<T>).
See the Rust Language Book on interior mutability.
Required Associated Types§
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.