6个版本 (1 个稳定版)
| 1.0.0 | 2024年4月4日 |
|---|---|
| 0.2.2 | 2022年12月6日 |
| 0.2.1 | 2020年4月26日 |
| 0.2.0 | 2020年3月24日 |
| 0.1.1 | 2020年3月22日 |
#374 在 数据结构
9,499 每月下载
用于 64 个crate(5 个直接使用)
12KB
199 行
可能是所有者值
| 文档 | crate信息 | 仓库 |
此crate提供了两个简单的包装器 Mown 和 MownMut,用于可以所有或借用的值。类型 Mown 是一个简单的 enum 类型,有两个构造函数
pub trait Borrowed {
type Owned: Borrow<Self>;
}
pub enum Mown<'a, T: Borrowed> {
Owned(T::Owned),
Borrowed(&'a T)
}
可变版本 MownMut 按照相同的定义,带有可变引用。这与标准的 Cow 类型非常相似,但无法将借用值转换为所有者值。这也与类似的crate boow 略有不同,因为 ToOwned trait 允许使用 Mown 与无尺寸类型(例如 Mown<str>)以及可变引用一起使用。
基本用法
Mown 类型的基本用例之一是在某些条件下想要重用一些输入借用值,或者使用自定义所有者值的情况。
use mown::Mown;
fn function(input_value: &String) -> Mown<String> {
if condition {
Mown::Borrowed(input_value)
} else {
let custom_value: String = "foo_".to_string() + input_value + "_bar";
Mown::Owned(custom_value)
}
}
还可以包装提供 ToOwned trait 的无尺寸类型。这是无尺寸 str 类型与有尺寸所有者类型 String 的情况。
use mown::Mown;
fn function(input_value: &str) -> Mown<str> {
if condition {
Mown::Borrowed(input_value)
} else {
let custom_value: String = "foo_".to_string() + input_value + "_bar";
Mown::Owned(custom_value)
}
}
许可证
许可以下任一项
- Apache License, Version 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- 麻省理工学院许可证(LICENSE-MIT 或 https://open-source.org.cn/licenses/MIT)
由您选择。
贡献
除非您明确说明,否则您根据Apache-2.0许可证定义的,旨在包含在作品中的任何有意贡献,将如上双许可,无任何附加条款或条件。