Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add proof of concept serde support with PathBuf#512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jamesmunns wants to merge6 commits intomain
base:main
Choose a base branch
Loading
frompoc-serde-support

Conversation

jamesmunns
Copy link

This is a proof of concept for supporting types fromasync_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)]structHmmm{ex: async_std::path::PathBuf,}fnmain(){println!("Hello, world!");}
# Cargo.toml[package]name ="serde-ex"version ="0.1.0"authors = ["James Munns <james.munns@ferrous-systems.com>"]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"

Leo1003 reacted with thumbs up emoji
@yoshuawuyts
Copy link
Contributor

@jamesmunns would it be possible to useserde-derive here instead so that we can call the featureserde?

@jamesmunns
Copy link
Author

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

@jamesmunns
Copy link
Author

Things that need to be decided now:

  1. What should the feature name be? justserde if we can depend onserde-derive? Something else?
  2. Should this be behind theunstable feature? e.g. requireserde-support ANDunstable be set for now?
  3. Are there any cases where a simpleflatten 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.

@yoshuawuyts
Copy link
Contributor

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

@jamesmunns
Copy link
Author

@yoshuawuyts ah, no, I think we need aserde 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

@ghost
Copy link

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

See also:rust-lang/api-guidelines#180

@jamesmunns
Copy link
Author

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)]structHmmm{ex: async_std::path::PathBuf,}fnmain(){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:5note: 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
Copy link
Author

But it does work for newtypes!c70f00e switchesPathBuf to be a newtype, rather than a struct containing aninner 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 implSerialize/Deserialize

@yoshuawuyts
Copy link
Contributor

yoshuawuyts commentedNov 14, 2019
edited
Loading

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.

@yoshuawuytsyoshuawuyts added this to the1.1.0 milestoneNov 20, 2019
@jamesmunns
Copy link
Author

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

@yoshuawuytsyoshuawuyts requested a review froma userNovember 25, 2019 19:39
Copy link

@ghostghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@yoshuawuyts and I discussed this during triage.

I'm 👍 with the idea of making more and more async-std types implementSerialize andDeserialize.

I don't think those implementations should also go behind theunstable flag, that would a bit too cautious to the point where stabilizations becomes an nuisance with little benefit.

Approving. Thanks for submitting the PR! :)

@ghost
Copy link

@dtolnay Do you think we should name the feature flagserde orserde1?

Copy link

@dtolnaydtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I recommend sticking with "serde".

Two thoughts:

  • I am skeptical of making this a default feature. What fraction of users need this enabled? Or is it default only because you already depend on something that depends on serde?
  • I am skeptical of using serde_derive. A pair of Serialize/Deserialize impls for a wrapper type that forwards to the inner PathBuf's impls are ~15 lines to write. Is that a better tradeoff?

@yoshuawuyts
Copy link
Contributor

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

Agreed; this should only be available when theserde feature is enabled. We currently don't depend onserde 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!

@maisiliym
Copy link

maisiliym commentedOct 20, 2020
edited
Loading

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 withPath showed.
Indeed, manual impls will be needed.
The case has been brought to serde:serde-rs/serde#1913

@hoijui
Copy link

hoijui commentedJun 2, 2024
edited
Loading

How is the state of this?
As I understood from the conversation here, it is ready to be merged.. no?

( personally need serde support exactly for PathBuf)

@poly2it
Copy link

It would be great to have this merged. I am also in need of this feature for PathBuf.

hoijui reacted with thumbs up emoji

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

@dtolnaydtolnaydtolnay left review comments

@yoshuawuytsyoshuawuytsyoshuawuyts approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
1.1.0
Development

Successfully merging this pull request may close these issues.

6 participants
@jamesmunns@yoshuawuyts@maisiliym@hoijui@poly2it@dtolnay

[8]ページ先頭

©2009-2025 Movatter.jp