2 releases
0.1.1 | Apr 14, 2023 |
---|---|
0.1.0 | Apr 13, 2023 |
#4 in #possibly
6KB
72 lines
Broption
For when you need more bro's in your ption's.
lib.rs
:
Branded Option. for when you need bro's in your ption's.
Possibly unsound. Designed for use with selfref
.
Examples
use broption::BOption;
// define a resource that needs a separate initialization step.
struct Foo<'bro> {
name: BOption<'bro, Box<str>>,
}
// define a context wrapper for our resource.
struct Ctx;
impl broption::Wrapper for Ctx {
type Kind<'bro> = Foo<'bro>;
}
// create an "initialization context".
let initialized = BOption::factory::<Ctx, _>(|factory| {
// create an uninitialized resource.
let mut maybeinit = Foo {
name: factory.new_none(),
};
// initialize the resource.
factory.init(&mut maybeinit.name, Box::from("hello"));
// return the hopefully-initialized resource.
maybeinit
});
// use the initialized resource
assert_eq!(&**initialized.name, "hello");
Failing to correctly initialize the resource panics:
use broption::BOption;
// define a resource that needs a separate initialization step.
struct Foo<'bro> {
name: BOption<'bro, Box<str>>,
}
// define a context wrapper for our resource.
struct Ctx;
impl broption::Wrapper for Ctx {
type Kind<'bro> = Foo<'bro>;
}
// create an "initialization context".
let initialized = BOption::factory::<Ctx, _>(|factory| {
// create an uninitialized resource.
let mut maybeinit = Foo {
name: factory.new_none(),
};
// return the uninitialized resource.
maybeinit
});