pub fn to_owned<T>(value: T) -> T::Ownedwhere
T: ToOwned,
Expand description
Convert a value to owned.
This helper function is provided so that you don’t have to have the
ToOwned
trait in scope, and make it explicit when this crate is being
used since this conversion is not a cheap operation in this crate.
Using this also prevents conflicts with the built-in
std::borrow::ToOwned
which is in the prelude.
§Examples
#[borrowme]
struct Word<'a> {
text: &'a str,
}
impl OwnedWord {
fn new(text: &str) -> Self {
Self { text: text.to_owned() }
}
}
#[borrowme]
#[derive(Default)]
struct Dictionary<'a> {
words: Vec<Word<'a>>,
}
fn uppercase(dictionary: OwnedDictionary) -> Vec<String> {
let mut out = Vec::new();
for word in dictionary.words {
out.push(word.text.to_uppercase());
}
out
}
let mut dictionary = Dictionary::default();
dictionary.words.push(Word { text: "Hello" });
dictionary.words.push(Word { text: "World" });
let out = uppercase(borrowme::to_owned(dictionary));
assert_eq!(out[0], "HELLO");
assert_eq!(out[1], "WORLD");