pub fn default_box<T: Default>() -> Box<T>Expand description
Similar to the make_box API, the default_box API is a wrapper over the unsafer version of the
directly-to-the-heap ones. If the struct wrapped in the box has implemented the Default trait,
then one can call this API that will invoke the Default::default to initialize the object, such
that the caller won’t need to supply a closure to initialize all the fields.
§Examples
Create the box on the heap with the default implementation of the struct
use syncpool::default_box;
use std::vec;
struct BigStruct {
a: u32,
b: u32,
c: Vec<u8>,
}
impl Default for BigStruct {
fn default() -> Self {
BigStruct {
a: 1,
b: 42,
c: vec::from_elem(0, 0x1_000_000),
}
}
}
// create the object directly on the heap
let boxed: Box<BigStruct> = default_box();
// the fields are now valid
assert_eq!(boxed.c.len(), 0x1_000_000);
assert_eq!(boxed.a, 1);
assert_eq!(boxed.b, 42);