async-std icon indicating copy to clipboard operation
async-std copied to clipboard

Add proof of concept serde support with PathBuf

Open jamesmunns opened this issue 5 years ago • 14 comments

This is a proof of concept for supporting types from async_std to be used with Serde. For now I have only implemented support for PathBuf, but if you like this, we can start rolling it out to all of the wrapped types.

// src/lib.rs:
use async_std;
use serde::{self, Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Hmmm {
    ex: async_std::path::PathBuf,
}

fn main() {
    println!("Hello, world!");
}
# Cargo.toml
[package]
name = "serde-ex"
version = "0.1.0"
authors = ["James Munns <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies.async-std]
version = "1.0"
path = "../async-std"

[dependencies.serde]
features = ["derive"]
version = "1.0"

jamesmunns avatar Nov 11 '19 23:11 jamesmunns

@jamesmunns would it be possible to use serde-derive here instead so that we can call the feature serde?

yoshuawuyts avatar Nov 12 '19 00:11 yoshuawuyts

@yoshuawuyts I think so! It's a habit of mine to just use serde as a dependency, but it's likely not needed here, just serde-derive.

jamesmunns avatar Nov 12 '19 00:11 jamesmunns

Things that need to be decided now:

  1. What should the feature name be? just serde if we can depend on serde-derive? Something else?
  2. Should this be behind the unstable feature? e.g. require serde-support AND unstable be set for now?
  3. Are there any cases where a simple flatten won't work? Maybe wrapper types with more than one field?

Once we decide and correct those, we can probably land this with just PathBuf supported, then start incrementally covering more data types. Or we can hit them all at once in this PR.

jamesmunns avatar Nov 12 '19 00:11 jamesmunns

Another thing we may want to consider is adding an entry to our "features" section in lib.rs: https://docs.rs/async-std/1.0.0/async_std/#features. This seems like something people may want to be aware of.

yoshuawuyts avatar Nov 12 '19 00:11 yoshuawuyts

@yoshuawuyts ah, no, I think we need a serde dependency as well. Otherwise I get:

    Checking async-std v1.0.0 (/tmp/async-std)
error[E0463]: can't find crate for `serde`
  --> /tmp/async-std/src/path/pathbuf.rs:22:49
   |
22 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
   |                                                 ^^^^^^^^^^^ can't find crate

jamesmunns avatar Nov 12 '19 00:11 jamesmunns

Should we put serde support behind a new feature flag, perhaps named serde1?

See also: https://github.com/rust-lang/api-guidelines/issues/180

ghost avatar Nov 12 '19 01:11 ghost

Hmm, this needs a deeper look. My first approach won't work.

use async_std;
use serde_json::to_string;
use serde::{self, Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Hmmm {
    ex: async_std::path::PathBuf,
}

fn main() {
    dbg!(to_string(&Hmmm {
        ex: async_std::path::PathBuf::new(),
    }).unwrap());
}
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("can only flatten structs and maps (got a string)", line: 0, column: 0)', src/libcore/result.rs:1165:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

There's likely still a way, just probably a bit less elegant. Merits further investigation.

jamesmunns avatar Nov 12 '19 02:11 jamesmunns

But it does work for newtypes! https://github.com/async-rs/async-std/pull/512/commits/c70f00e915053c9dc43ef10cbe72fe3fc7a3a990 switches PathBuf to be a newtype, rather than a struct containing an inner field.

[src/main.rs:11] to_string(&Hmmm{ex: async_std::path::PathBuf::new(),}).unwrap() = "{\"ex\":\"\"}"

I don't think this needs to be a breaking change? And for fields where a newtype doesn't work, we can always manually impl Serialize/Deserialize

jamesmunns avatar Nov 12 '19 02:11 jamesmunns

I don't think this needs to be a breaking change?

We've never provided guarantees about internals, so indeed don't think it's a breaking change.

I think this PR looks great; but probably want to give @stjepang a chance to sign off on it too before we merge.

yoshuawuyts avatar Nov 14 '19 21:11 yoshuawuyts

@stjepang just let me know if you'd prefer serde or serde1 as the feature flag, then I think we can land this, and start adding serde support to other structures.

jamesmunns avatar Nov 20 '19 23:11 jamesmunns

@dtolnay Do you think we should name the feature flag serde or serde1?

ghost avatar Dec 13 '19 12:12 ghost

I am skeptical of making this a default feature. (...)

Agreed; this should only be available when the serde feature is enabled. We currently don't depend on serde anywhere else, so it's indeed a new dep.

I am skeptical of using serde_derive. (...)

Oh, yeah a manual impl def sounds better here. Thanks for suggesting!

yoshuawuyts avatar Dec 17 '19 14:12 yoshuawuyts

Status? Is anyone working on a PR to convert all wrapper types to newtypes, with serde feature?

Edit: That approach wont work in every case, as quick experiment with Path showed. Indeed, manual impls will be needed. The case has been brought to serde: https://github.com/serde-rs/serde/issues/1913

maisiliym avatar Oct 20 '20 04:10 maisiliym