#wrapper #borrow #reference #own #container

mown

Maybe owned values. Wrappers for values that can be either owned or borrowed.

5 releases

0.2.2 Dec 6, 2022
0.2.1 Apr 26, 2020
0.2.0 Mar 24, 2020
0.1.1 Mar 22, 2020
0.1.0 Mar 22, 2020

#782 in Data structures

Download history 1378/week @ 2023-11-05 1161/week @ 2023-11-12 1131/week @ 2023-11-19 1503/week @ 2023-11-26 1369/week @ 2023-12-03 1224/week @ 2023-12-10 1259/week @ 2023-12-17 590/week @ 2023-12-24 1250/week @ 2023-12-31 881/week @ 2024-01-07 1270/week @ 2024-01-14 1456/week @ 2024-01-21 1931/week @ 2024-01-28 1930/week @ 2024-02-04 1691/week @ 2024-02-11 1825/week @ 2024-02-18

7,481 downloads per month
Used in 40 crates (6 directly)

MIT/Apache

13KB
193 lines

Maybe Owned values

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 ToOwned {
	type Owned: Borrow<Self>;
}

pub enum Mown<'a, T: ToOwned> {
	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.

Basic Usage

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> {
	let value = if condition {
		Mown::Borrowed(input_value)
	} else {
		let custom_value: String = "foo_".to_string() + input_value.to_string() + "_bar" ;
		Mown::Owned(custom_value)
	}
}

License

Licensed under either of

at your option.

Contribution

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.

No runtime deps