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

Implement Serialize/Deserialize for Infallible #2073

Closed
TehPers opened this issue Aug 12, 2021 · 3 comments
Closed

Implement Serialize/Deserialize for Infallible #2073

TehPers opened this issue Aug 12, 2021 · 3 comments

Comments

@TehPers
Copy link

TehPers commented Aug 12, 2021

Suppose I have an API where a value can only be null. In Rust, the semantic way to represent null is via Option::<T>::None, but it's unclear what T should be because the value can never be Some(value), only None.

I think it would be useful to be able to use Option<Infallible> (and possibly Option<!>) to represent these values. One way to do this is to implement Serialize directly for Infallible:

impl Serialize for Infallible {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {}
    }
}

Since an instance of Infallible should theoretically never be serialized, this would allow Option<Infallible> to serialize as None.

It would also be nice to be able to deserialize Option<Infallible>:

impl<'de> Deserialize<'de> for Infallible {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Err(de::Error::custom("value can never be constructed"))
    }
}

As a temporary solution, it's possible to define an empty enum and implement these traits on it.

@TehPers TehPers changed the title Implement Serialize/Deserialize for Option<Infallible> Implement Serialize/Deserialize for Infallible Aug 12, 2021
@dtolnay
Copy link
Member

dtolnay commented Jan 23, 2022

For types with only one possible value, a unit struct seems more appropriate than something involving Infallible. In formats like JSON a unit struct will serialize to null, which sounds like is what you want.

#[derive(Serialize)]
struct Null;

@dtolnay dtolnay closed this as completed Jan 23, 2022
@zakarumych
Copy link

Implementing Serialize and Deserialize for Infallible is desired for generic enums.

Imagine enum like this.

#[derive(Serialize, Deserialize)]
enum Foo<T> {
  A,
  B,
  C(T),
}

it may be desirable to serialize and deserialize Foo<Infallible>.
Without it, users have to define ad-hoc

#[derive(Serialize, Deserialize)]
enum Never {}

and use Foo<Never>

@arctic-hen7
Copy link

I'm working on a system that accepts user state in the form of a Result<T, E> (which can't be changed due to reactivity constraints), and, although most of the time there will be an error type, some cases will use Infallible. It would be extremely helpful to have this implementation!

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

4 participants