#serde #detach #borrowing #type #cow-str #static #deserialise

serde-detach

Deserialise types containing e.g. Cow<str> without borrowing

1 unstable release

0.0.1 Aug 26, 2020

#42 in #borrowing

Download history 322/week @ 2024-03-14 289/week @ 2024-03-21 188/week @ 2024-03-28 144/week @ 2024-04-04 194/week @ 2024-04-11 157/week @ 2024-04-18 106/week @ 2024-04-25 145/week @ 2024-05-02 133/week @ 2024-05-09 162/week @ 2024-05-16 170/week @ 2024-05-23 172/week @ 2024-05-30 193/week @ 2024-06-06 149/week @ 2024-06-13 134/week @ 2024-06-20 73/week @ 2024-06-27

577 downloads per month
Used in 2 crates

MPL-2.0 license

12KB
261 lines

Nudges Serde to deserialise into fully owned ('static) instances where this is possible.

This is primarily useful when you have a structure that can optionally borrow the input (Think Cow<str> or Cow<[u8]>.), but a deserialiser that requires DeserializeOwned (which unfortunately seems to be common).

Deserialising borrow-only types (like plain references) naturally leads to a runtime error with this method.

Example

Given:

use {
    serde_detach::detach,
    serde_object::Object,
    serde_taml::de::from_str,
};

let input = "key: \"value\"".to_string();

This does not compile, since Object tries to borrow from the input:

let object: Object<'static> = from_str(&input, &mut ())?;
//          ---------------            ^^^^^^ borrowed value does not live long enough
//          |
//          type annotation requires that `input` is borrowed for `'static`

This works:

let object: Object<'static> = from_str(&input, &mut ()).map(detach)?;

Dependencies

~140–385KB