pub unsafe trait Zero: Sized { }Expand description
Trait indicating whether a value full of zeroes is valid.
This trait is used to enable the Box::new_zeroed method for types where
it’s safe to use.
§Safety
Do not implement this trait for types where a raw byte array of 0 doesn’t represent a valid value for the type. Please double check it is valid and corresponds to what you want.
§Examples
extern crate boxext;
use boxext::{BoxExt, Zero};
#[derive(Debug, PartialEq)]
struct Foo(usize);
unsafe impl Zero for Foo {}
fn main() {
// equivalent to `Box::new(Foo(0))`
let buf: Box<Foo> = Box::new_zeroed();
assert_eq!(*buf, Foo(0));
}For convenience, a boxext_derive crate is provided that provides a
custom derive for Zero.
extern crate boxext;
#[macro_use]
extern crate boxext_derive;
use boxext::BoxExt;
#[derive(Zero, Debug, PartialEq)]
struct Foo(usize);
fn main() {
// equivalent to `Box::new(Foo(0))`
let buf: Box<Foo> = Box::new_zeroed();
assert_eq!(*buf, Foo(0));
}ⓘ
extern crate boxext;
#[macro_use]
extern crate boxext_derive;
use boxext::BoxExt;
#[derive(Zero)]
// ^ the trait `boxext::Zero` is not implemented for `Bar`
struct Foo(Bar);
struct Bar;
fn main() {
// equivalent to `Box::new(Foo(0))`
let buf: Box<Foo> = Box::new_zeroed();
}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.