Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow deserialize() to have a non-Self return type #2735

Closed
vrurg opened this issue May 3, 2024 · 1 comment
Closed

Allow deserialize() to have a non-Self return type #2735

vrurg opened this issue May 3, 2024 · 1 comment

Comments

@vrurg
Copy link

vrurg commented May 3, 2024

Within a task I'm currently trying to solve there is a need for async structs to initialize themselves and produce not just Self but Arc<Self>. At the same time I want them to support serde when necessary. But here comes a problem because neither approach I managed to dig up with google allows to, say, serde_toml::from_str(source) return an Arc<Self>. Perhaps remote parameter can help, but I'm not sure how well would it blend with from. Besides, it introduces another, rather unnecessary, layer of abstraction with introduction of RemoteSelf<Arc<Self>>.

Instead, for something like:

#[serde(from = "__FooShadow")]
struct Foo {...}

It would be much simpler and cleaner to implement something like the following pseudo-code:

impl<'de> Deserialize<'de> for Foo {
    fn deserialize<__D>(deserializer: __D) -> Result<Arc<Self>, __D::Error> {
        let shadow = __FooShadow::deserialize(deserializer);
        let me: Arc<Self> = Self::from(shadow);
        Ok(me)
    }
}
@dtolnay
Copy link
Member

dtolnay commented May 15, 2024

It would be interesting to explore designs for this in a different serialization library, but I don't see this being something that would change in serde.

@dtolnay dtolnay closed this as completed May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants