#options #data #replace

可选

此软件包提供了多个类似于 Option 的原始类型

18 个版本

使用旧的 Rust 2015

0.5.0 2018 年 11 月 25 日
0.4.3 2018 年 7 月 18 日
0.4.2 2018 年 6 月 3 日
0.3.0 2017 年 9 月 9 日
0.0.11 2015 年 7 月 31 日

#259 in 数据结构

Download history 2688/week @ 2023-12-14 1916/week @ 2023-12-21 1978/week @ 2023-12-28 2026/week @ 2024-01-04 1982/week @ 2024-01-11 2118/week @ 2024-01-18 2120/week @ 2024-01-25 1888/week @ 2024-02-01 1995/week @ 2024-02-08 2634/week @ 2024-02-15 1930/week @ 2024-02-22 1961/week @ 2024-02-29 2067/week @ 2024-03-07 1841/week @ 2024-03-14 2407/week @ 2024-03-21 2156/week @ 2024-03-28

8,699 每月下载量
22 个软件包中使用了 (直接使用 11)

MIT/Apache

61KB
1K SLoC

可选

Build Status License: MIT/Apache

此包旨在提供一些类似 Option<_> 的值,允许空间优化的表示。

目前有一个类型 OptionBool。它非常类似于 Option<bool> (实际上大部分复制了其接口)。只有少数额外的函数,在 Option<bool> 会接收或返回另一个 Option<bool> 时,使用 _bool 前缀来接收/返回 OptionBool

请注意,由于内部转换,通常无法创建 iter_mut()as_mut_slice() 方法。因此,这些方法无法支持。

OptionBool 不仅与 Option<bool> 相比提高了空间效率,而且在我的基准测试中,大多数方法也运行得稍微快一些。截至 2016 年 1 月 22 日,在运行 LUbuntu 15.04 的 Intel(R) Core(TM)2 Quad CPU Q8400 @ 2.66GHz 上,使用 Rust 1.8.0-nightly 和 3.19.0-23-generic 内核,我得到了以下结果

test bench_and_bool_opt        ... bench:       1,490 ns/iter (+/- 38)
test bench_and_opt             ... bench:       1,667 ns/iter (+/- 37)
test bench_and_std             ... bench:       1,695 ns/iter (+/- 30)
test bench_as_slice_iter_opt   ... bench:       2,211 ns/iter (+/- 16)
test bench_as_slice_opt        ... bench:       1,507 ns/iter (+/- 23)
test bench_from_opt            ... bench:       1,795 ns/iter (+/- 137)
test bench_is_some_opt         ... bench:       1,443 ns/iter (+/- 17)
test bench_is_some_std         ... bench:       1,659 ns/iter (+/- 17)
test bench_iter_opt            ... bench:       2,212 ns/iter (+/- 39)
test bench_iter_std            ... bench:       1,553 ns/iter (+/- 22)
test bench_map_invert_opt      ... bench:       2,918 ns/iter (+/- 35)
test bench_map_invert_opt_bool ... bench:       2,063 ns/iter (+/- 23)
test bench_map_invert_std      ... bench:       2,510 ns/iter (+/- 36)
test bench_map_or_else_opt     ... bench:       2,268 ns/iter (+/- 35)
test bench_map_or_else_std     ... bench:       2,309 ns/iter (+/- 33)
test bench_map_or_opt          ... bench:       1,669 ns/iter (+/- 19)
test bench_map_or_std          ... bench:       1,975 ns/iter (+/- 26)
test bench_or_bool_opt         ... bench:       1,424 ns/iter (+/- 33)
test bench_or_else_bool_opt    ... bench:       1,654 ns/iter (+/- 30)
test bench_or_else_opt         ... bench:       1,766 ns/iter (+/- 24)
test bench_or_else_std         ... bench:       1,529 ns/iter (+/- 13)
test bench_or_opt              ... bench:       1,806 ns/iter (+/- 50)
test bench_or_std              ... bench:       1,729 ns/iter (+/- 28)
test bench_to_opt              ... bench:       1,979 ns/iter (+/- 87)
test bench_unwrap_opt          ... bench:       1,700 ns/iter (+/- 29)
test bench_unwrap_or_else_opt  ... bench:       1,666 ns/iter (+/- 19)
test bench_unwrap_or_else_std  ... bench:       2,149 ns/iter (+/- 31)
test bench_unwrap_or_opt       ... bench:       1,653 ns/iter (+/- 23)
test bench_unwrap_or_std       ... bench:       2,124 ns/iter (+/- 30)
test bench_unwrap_std          ... bench:       1,645 ns/iter (+/- 20)

[...]

test bench_as_slice_iter_opt_f32 ... bench:       2,090 ns/iter (+/- 35)
test bench_as_slice_iter_opt_f64 ... bench:       1,870 ns/iter (+/- 33)
test bench_as_slice_iter_opt_i16 ... bench:       1,937 ns/iter (+/- 24)
test bench_as_slice_iter_opt_i32 ... bench:       2,028 ns/iter (+/- 47)
test bench_as_slice_iter_opt_i64 ... bench:       1,680 ns/iter (+/- 32)
test bench_as_slice_iter_opt_i8  ... bench:       2,032 ns/iter (+/- 16)
test bench_as_slice_iter_opt_u16 ... bench:       1,937 ns/iter (+/- 28)
test bench_as_slice_iter_opt_u32 ... bench:       2,030 ns/iter (+/- 59)
test bench_as_slice_iter_opt_u64 ... bench:       1,775 ns/iter (+/- 135)
test bench_as_slice_iter_opt_u8  ... bench:       2,031 ns/iter (+/- 23)
test bench_is_some_opt_f32       ... bench:       1,721 ns/iter (+/- 17)
test bench_is_some_opt_f64       ... bench:       1,721 ns/iter (+/- 24)
test bench_is_some_opt_u16       ... bench:       1,711 ns/iter (+/- 26)
test bench_is_some_opt_u32       ... bench:       1,677 ns/iter (+/- 25)
test bench_is_some_opt_u64       ... bench:       1,664 ns/iter (+/- 31)
test bench_is_some_opt_u8        ... bench:       1,724 ns/iter (+/- 22)
test bench_is_some_std_f32       ... bench:       1,466 ns/iter (+/- 22)
test bench_is_some_std_f64       ... bench:       1,465 ns/iter (+/- 18)
test bench_is_some_std_u16       ... bench:       1,470 ns/iter (+/- 29)
test bench_is_some_std_u32       ... bench:       1,465 ns/iter (+/- 18)
test bench_is_some_std_u64       ... bench:       1,474 ns/iter (+/- 61)
test bench_is_some_std_u8        ... bench:       1,850 ns/iter (+/- 35)
test bench_iter_opt_f32          ... bench:       1,828 ns/iter (+/- 17)
test bench_iter_opt_f64          ... bench:       1,845 ns/iter (+/- 25)
test bench_iter_opt_i16          ... bench:       1,755 ns/iter (+/- 17)
test bench_iter_opt_i32          ... bench:       1,755 ns/iter (+/- 18)
test bench_iter_opt_i64          ... bench:       1,547 ns/iter (+/- 43)
test bench_iter_opt_i8           ... bench:       1,754 ns/iter (+/- 17)
test bench_iter_opt_u16          ... bench:       1,754 ns/iter (+/- 21)
test bench_iter_opt_u32          ... bench:       1,563 ns/iter (+/- 16)
test bench_iter_opt_u64          ... bench:       1,534 ns/iter (+/- 20)
test bench_iter_opt_u8           ... bench:       1,754 ns/iter (+/- 18)
test bench_iter_std_f32          ... bench:       1,563 ns/iter (+/- 39)
test bench_iter_std_f64          ... bench:       1,587 ns/iter (+/- 17)
test bench_iter_std_i16          ... bench:       1,487 ns/iter (+/- 32)
test bench_iter_std_i32          ... bench:       1,587 ns/iter (+/- 20)
test bench_iter_std_i64          ... bench:       1,587 ns/iter (+/- 25)
test bench_iter_std_i8           ... bench:       1,631 ns/iter (+/- 98)
test bench_iter_std_u16          ... bench:       1,488 ns/iter (+/- 23)
test bench_iter_std_u32          ... bench:       1,590 ns/iter (+/- 59)
test bench_iter_std_u64          ... bench:       1,587 ns/iter (+/- 25)
test bench_iter_std_u8           ... bench:       1,623 ns/iter (+/- 72)
test bench_map_opt_f32           ... bench:       1,959 ns/iter (+/- 67)
test bench_map_opt_f64           ... bench:       1,654 ns/iter (+/- 40)
test bench_map_opt_u16           ... bench:       1,789 ns/iter (+/- 31)
test bench_map_opt_u32           ... bench:       2,431 ns/iter (+/- 43)
test bench_map_opt_u64           ... bench:       1,432 ns/iter (+/- 32)
test bench_map_opt_u8            ... bench:       1,813 ns/iter (+/- 18)
test bench_map_or_opt_f32        ... bench:       1,623 ns/iter (+/- 17)
test bench_map_or_opt_f64        ... bench:       1,669 ns/iter (+/- 22)
test bench_map_or_opt_u16        ... bench:       1,717 ns/iter (+/- 18)
test bench_map_or_opt_u32        ... bench:       1,640 ns/iter (+/- 19)
test bench_map_or_opt_u64        ... bench:       1,579 ns/iter (+/- 40)
test bench_map_or_opt_u8         ... bench:       1,814 ns/iter (+/- 20)
test bench_map_or_std_f32        ... bench:       1,537 ns/iter (+/- 28)
test bench_map_or_std_f64        ... bench:       1,777 ns/iter (+/- 28)
test bench_map_or_std_u16        ... bench:       1,931 ns/iter (+/- 37)
test bench_map_or_std_u32        ... bench:       1,807 ns/iter (+/- 22)
test bench_map_or_std_u64        ... bench:       1,786 ns/iter (+/- 15)
test bench_map_or_std_u8         ... bench:       2,010 ns/iter (+/- 19)
test bench_map_std_f32           ... bench:       1,811 ns/iter (+/- 37)
test bench_map_std_f64           ... bench:       1,925 ns/iter (+/- 45)
test bench_map_std_u16           ... bench:       2,357 ns/iter (+/- 23)
test bench_map_std_u32           ... bench:       2,262 ns/iter (+/- 102)
test bench_map_std_u64           ... bench:       1,933 ns/iter (+/- 48)
test bench_map_std_u8            ... bench:       2,545 ns/iter (+/- 65)

除了 OptionBool 之外,还有一个类似 Option 但无需额外存储空间的 struct Optioned<T>。它为 i8..64u8..64isizeusizef32f64 提供了实现。为自己的类型实现它也很简单(查看 Noned 特性)。对于整型,None 的值为它们的 MIN,对于无符号类型是它们的 MAX,对于浮点数是 NAN(无论符号如何)。

文档

API 文档

您还可以查看非常详尽的 Option 类型,该 crate 以其为基础。

许可证

以下许可证之一

任选其一。

贡献

除非您明确声明,否则根据 Apache-2.0 许可证定义的,您提交给本作品包含在内的任何有意贡献,将按照上述方式双许可,没有任何附加条款或条件。

依赖项

~175KB