Crates.io | mown |
lib.rs | mown |
version | 1.0.0 |
source | src |
created_at | 2020-03-22 14:14:11.861732 |
updated_at | 2024-04-04 20:36:00.865909 |
description | Maybe owned values. Wrappers for values that can be either owned or borrowed. |
homepage | |
repository | https://github.com/timothee-haudebourg/mown |
max_upload_size | |
id | 221405 |
size | 24,411 |
Documentation | Crate informations | Repository |
This crate provides two simple wrappers
Mown
and
MownMut
for values that can be either owned or borrowed.
The type Mown
is an simple enum
type with two constructors:
pub trait Borrowed {
type Owned: Borrow<Self>;
}
pub enum Mown<'a, T: Borrowed> {
Owned(T::Owned),
Borrowed(&'a T)
}
The mutable version MownMut
follows the same definition with a mutable
reference.
This is very similar to the standard
Cow
type, except that it is not possible to transform a borrowed value into an owned
one.
This is also slightly different from the similar crate
boow
since the [ToOwned
] trait allow for the use of Mown
with unsized types
(for instance Mown<str>
) and with mutable references.
One basic use case for the Mown
type is the situation where one wants to
reuse some input borrowed value under some condition, or then use a custom
owned value.
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)
}
}
One can also wrap unsized types for which the provided [ToOwned
]
trait has been implemented.
This is the case for the unsized str
type with the sized owned type 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)
}
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.