Expand description
cervine::Cow
is an alloc::borrow::Cow
alternative that has different generic type constraints.
§Features
"serde"
: Implements serde::Deserialize
and serde::Serialize
on Cow
.
§Examples
Same type (T = R = [bool; 2]
):
use cervine::Cow;
use rand::prelude::*;
use std::borrow::Borrow as _;
let data = [true, false];
let mut cow = Cow::Borrowed(&data);
if thread_rng().gen() {
cow = Cow::Owned([false, true]);
}
let array_ref: &[bool; 2] = cow.borrow();
Different types (T = String
and R = str
):
use cervine::Cow;
use rand::prelude::*;
use smartstring::alias::String;
let mut cow = Cow::Borrowed("borrowed");
if thread_rng().gen() {
cow = Cow::Owned(String::from("owned"));
}
let str_ref: &str = cow.as_ref();
Enums§
Cow
is a clone-on-write smart pointer largely analogous toalloc::borrow::Cow
, with one key difference:
Instead of requiringToOwned
, the owned and borrowed type are both configurable and most methods requireT: Borrow<R>
.