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

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 387/week @ 2024-10-06 437/week @ 2024-10-13 336/week @ 2024-10-20 222/week @ 2024-10-27 463/week @ 2024-11-03 392/week @ 2024-11-10 287/week @ 2024-11-17 555/week @ 2024-11-24 338/week @ 2024-12-01 470/week @ 2024-12-08 361/week @ 2024-12-15 96/week @ 2024-12-22 83/week @ 2024-12-29 459/week @ 2025-01-05 1092/week @ 2025-01-12 1030/week @ 2025-01-19

2,668 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

~130–360KB