Skip to content

Commit 228b01d

Browse files
committed
workaround hrtb Fn* not being (de)serializable
1 parent c3b0e6a commit 228b01d

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/convenience.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,16 @@ impl<'a, Args, Output> AsRef<Self> for dyn FnOnce<Args, Output = Output> + Send
704704
self
705705
}
706706
}
707+
impl<'a, A, Output> AsRef<Self> for dyn for<'r> FnOnce<(&'r A,), Output = Output> + 'a {
708+
fn as_ref(&self) -> &Self {
709+
self
710+
}
711+
}
712+
impl<'a, A, Output> AsRef<Self> for dyn for<'r> FnOnce<(&'r A,), Output = Output> + Send + 'a {
713+
fn as_ref(&self) -> &Self {
714+
self
715+
}
716+
}
707717

708718
impl<Args: 'static, Output: 'static> serde::ser::Serialize for dyn FnOnce<Args, Output = Output> {
709719
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
@@ -744,6 +754,50 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
744754
.map(|x| x.0)
745755
}
746756
}
757+
impl<A: 'static, Output: 'static> serde::ser::Serialize
758+
for dyn for<'r> FnOnce<(&'r A,), Output = Output>
759+
{
760+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
761+
where
762+
S: serde::Serializer,
763+
{
764+
serialize(self, serializer)
765+
}
766+
}
767+
impl<'de, A: 'static, Output: 'static> serde::de::Deserialize<'de>
768+
for boxed::Box<dyn for<'r> FnOnce<(&'r A,), Output = Output> + 'static>
769+
{
770+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
771+
where
772+
D: serde::Deserializer<'de>,
773+
{
774+
<Box<dyn for<'r> FnOnce<(&'r A,), Output = Output> + 'static>>::deserialize(deserializer)
775+
.map(|x| x.0)
776+
}
777+
}
778+
impl<A: 'static, Output: 'static> serde::ser::Serialize
779+
for dyn for<'r> FnOnce<(&'r A,), Output = Output> + Send
780+
{
781+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
782+
where
783+
S: serde::Serializer,
784+
{
785+
serialize(self, serializer)
786+
}
787+
}
788+
impl<'de, A: 'static, Output: 'static> serde::de::Deserialize<'de>
789+
for boxed::Box<dyn for<'r> FnOnce<(&'r A,), Output = Output> + Send + 'static>
790+
{
791+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
792+
where
793+
D: serde::Deserializer<'de>,
794+
{
795+
<Box<dyn for<'r> FnOnce<(&'r A,), Output = Output> + Send + 'static>>::deserialize(
796+
deserializer,
797+
)
798+
.map(|x| x.0)
799+
}
800+
}
747801

748802
/// A convenience trait implemented on all (de)serializable implementors of [`std::ops::FnMut`].
749803
///

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
clippy::pedantic
121121
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
122122
#![allow(clippy::must_use_candidate, clippy::missing_errors_doc)]
123+
// At some point this, and the HRTB Fn* impls, will be invalid
124+
// See https://github.com/rust-lang/rust/issues/56105
125+
#![allow(coherence_leak_check)]
123126

124127
mod convenience;
125128

0 commit comments

Comments
 (0)