laxcow

Crates.iolaxcow
lib.rslaxcow
version0.2.0
sourcesrc
created_at2022-11-20 13:28:17.033759
updated_at2023-04-28 17:27:34.319073
descriptionClone-on-write smart pointer with relaxed trait constraints
homepagehttps://github.com/kauppie/laxcow/tree/main
repositoryhttps://github.com/kauppie/laxcow/tree/main
max_upload_size
id719117
size30,463
Elias Kauppi (kauppie)

documentation

https://docs.rs/laxcow

README

LaxCow

CI status Crate Docs License

Clone-on-write smart pointer similar to Cow, but with relaxed trait constraints. Crate is totally no_std.

Examples

Simple usage

use laxcow::LaxCow;

let lc = LaxCow::Borrowed("foobar");

let lc2 = lc.clone();
assert_eq!(lc2, LaxCow::Borrowed("foobar"));

let owned = lc.into_owned();
assert_eq!(owned, "foobar".to_owned());

Usage not possible with Cow

Storing a borrowed struct which doesn't implement Clone. This is possible because LaxCow::Owned variant is not restricted by the LaxCow::Borrowed variant via ToOwned trait.

use laxcow::LaxCow;

struct Foo;

// We don't care about the owned type as it is not used.
let laxcow = LaxCow::<_, ()>::Borrowed(&Foo);

Cow definition by wrapping LaxCow

This example shows the difference between LaxCow and Cow. It makes Cow a struct, but only works here as an example.

use laxcow::LaxCow;

struct Cow<'a, T: ?Sized + ToOwned>(LaxCow::<'a, T, <T as ToOwned>::Owned>);

Usability without alloc

LaxCow can be used without alloc but this restricts the usage quite a bit as ToOwned is defined in alloc. Interaction with Cow also is not possible. It is also debatable if clone-on-write functionality is useful in no_std environment, but this crate tries not to make any assumptions about the usage.

License

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.

Commit count: 0

cargo fmt