#options #allocation #some #none

another-option

Another option data type; useful when allocations are expensive

2 releases

0.1.1 Dec 30, 2019
0.1.0 Dec 30, 2019

#1379 in Data structures

MIT license

14KB
303 lines

another-option

This package provides Opt<T> as an alternative to Option<T>. Why would you want another option? Opt provides advantages when:

  1. the generic type, T, is expensive to allocate, and
  2. mutation between None and Some(...) is frequent.

Examples

Since Rust's built-in Option<T> is an enum, it will drop its Some(...) value when None is assigned.

let mut option: Option<String> = Some(String::with_capacity(1024));
option = None; // drops the string
option = Some(String::with_capacity(1024)); // allocation

Since Opt<T> always owns the value, even when empty, the value can be reused without drops or allocations:

use crate::another_option::Opt;
let mut opt: Opt<String> = Opt::some(String::with_capacity(1024));
opt.map_in_place(|v| v.push_str("value"));
opt.set_none(); // does *not* drop the string
opt.set_some();
assert_eq!(opt.unwrap(), String::from("value"));

lib.rs:

another-option

This package provides Opt<T> as an alternative to Option<T>. Why would you want another option? Opt provides advantages when:

  1. the generic type, T, is expensive to allocate, and
  2. mutation between None and Some(...) is frequent.

Examples

Since Rust's built-in Option<T> is an enum, it will drop its Some(...) value when None is assigned.

let mut option: Option<String> = Some(String::with_capacity(1024));
option = None; // drops the string
option = Some(String::with_capacity(1024)); // allocation

Since Opt<T> always owns the value, even when empty, the value can be reused without drops or allocations:

use crate::another_option::Opt;
let mut opt: Opt<String> = Opt::some(String::with_capacity(1024));
opt.map_in_place(|v| v.push_str("value"));
opt.set_none(); // does *not* drop the string
opt.set_some();
assert_eq!(opt.unwrap(), String::from("value"));

No runtime deps