Skip to content

Deserializing to std::borrow::Cow<str> always allocates #1852

Description

@Vlad-Shcherbina
// [dependencies]
// serde_json = "1.0.41"
fn main() {
    let v = "hello";
    let s = serde_json::to_string(&v).unwrap();
    println!("{}", s);
    let v2: std::borrow::Cow<str> = serde_json::from_str(&s).unwrap();
    match v2 {
        std::borrow::Cow::Borrowed(..) => println!("borrowed"),
        std::borrow::Cow::Owned(..) => println!("owned"),
    }
}

Expected result

"hello"
borrowed

Because string hello appears in the data as is, with no escaping or reader buffer boundaries.

Actual result

"hello"
owned

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions