Is there a crate for generating a new struct with the same fields as another struct, but where each field's type is a reference?

50 points Tyilo 33 comments

Example of what I want:

// Envisioned usage:
#[field_refs(FooRef: Serialize)]
#[derive(Serialize, Deserialize)]
struct Foo {
    a: String,
    b: u8,
    c: bool,
}

// Generated code:
#[derive(Serialize)]
struct FooRef<'a> {
    a: &'a String,
    b: &'a u8,
    c: &'a bool,
}

Is there any crate that can do this or do I need to build one myself?

I want this for a mongodb document type, where I would like to insert a FooRef but get a Foo, without having to ensure that Foo and FooRef are in sync.