pub fn borrow<T>(value: &T) -> T::Target<'_>
Expand description
Borrow from the given value.
This helper function is provided so that you don’t have to have the
Borrow
trait in scope, and make it explicit when this crate is being
used since “borrowing” is not a cheap operation in this crate.
This also prevents conflicts with the built-in
Borrow
.
§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: Dictionary<'_>) -> Vec<String> {
let mut out = Vec::new();
for word in dictionary.words {
out.push(word.text.to_uppercase());
}
out
}
let mut dictionary = OwnedDictionary::default();
dictionary.words.push(OwnedWord::new("Hello"));
dictionary.words.push(OwnedWord::new("World"));
let out = uppercase(borrowme::borrow(&dictionary));
assert_eq!(out[0], "HELLO");
assert_eq!(out[1], "WORLD");