borrowme

github crates.io docs.rs build status

The missing compositional borrowing for Rust.

This crate provides an attribute macro which helps you achieve compositional borrowing. Roughly this means that you can convert a struct which has lifetimes into ones which does not and vice versa.

Note: See the #[borrowme] attribute for more documentation.

```rust

[borrowme::borrowme]

[derive(Debug, PartialEq, Eq)]

struct Word<'a> { #[owned(ty = String)] text: &'a str, #[owned(ty = Option)] lang: Option<&'a str>, }

let text = String::from("Hello World"); let lang = Some(String::from("eng"));

let word = Word { text: "Hello World", lang: lang.as_deref(), };

let word2: OwnedWord = borrowme::toowned(&word); asserteq!(word2.text.asstr(), word.text); asserteq!(word2.lang.as_deref(), word.lang);

let word3: Word<'> = borrowme::borrow(&word2); asserteq!(word3, word); ```


Rust comes with two sibling traits which both are responsible for converting something to an owned and a borrowed variant: ToOwned and Borrow.

These convert a type to a borrowed value to an owned one, let's think about it from a broader perspective: How to we convert a type which has lifetimes, to one which does not?

To this end this crate defines two similar traits: [ToOwned] and [Borrow]. These traits serve a similar purpose to the traits in std but are implemented differently. See their corresponding documentation for more details.