zlink_core/server/infallible.rs
1//! [`serde::Serialize`]-carrying wrapper around [`core::convert::Infallible`].
2
3use core::fmt::Debug;
4
5use serde::{Serialize, Serializer};
6
7/// [`serde::Serialize`]-carrying wrapper around [`core::convert::Infallible`].
8///
9/// Recommended choice for [`Service::ReplyError`] and [`Service::ReplyStreamError`] when the
10/// corresponding methods cannot fail. The [`service`] macro also picks it for
11/// [`Service::ReplyStreamError`] when no streaming method declares an error type.
12///
13/// It exists because of [serde-rs/serde#2740]: `core::convert::Infallible` has no `Serialize`
14/// impl in `serde`, which makes it unusable for trait associated types bounded by `Serialize`.
15/// This wrapper carries the missing impl. The inner [`core::convert::Infallible`] cannot be
16/// constructed, so neither can this — the `Serialize` impl is statically unreachable.
17///
18/// Once serde lands a built-in `Serialize` impl for `Infallible`, this wrapper will be
19/// deprecated in favor of `core::convert::Infallible`.
20///
21/// [`Service::ReplyError`]: super::service::Service::ReplyError
22/// [`Service::ReplyStreamError`]: super::service::Service::ReplyStreamError
23/// [`service`]: macro@crate::service
24/// [serde-rs/serde#2740]: https://github.com/serde-rs/serde/issues/2740
25#[derive(Debug)]
26pub struct Infallible(core::convert::Infallible);
27
28impl Serialize for Infallible {
29 fn serialize<S: Serializer>(&self, _serializer: S) -> Result<S::Ok, S::Error> {
30 unreachable!("`Infallible` is uninhabited, so this method can never run")
31 }
32}