|
1 |
| -use super::{deserialize, serialize, Deserialize, Serialize}; |
2 |
| -use serde; |
3 | 1 | use std::{
|
4 | 2 | any, borrow::{Borrow, BorrowMut}, boxed, error, fmt, marker, ops::{self, Deref, DerefMut}, rc, sync
|
5 | 3 | };
|
6 | 4 |
|
| 5 | +use super::{deserialize, serialize, Deserialize, Serialize}; |
| 6 | + |
7 | 7 | /// Convenience wrapper around [std::boxed::Box<T>](std::boxed::Box) that automatically uses `serde_traitobject` for (de)serialization.
|
8 | 8 | #[derive(Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
9 | 9 | pub struct Box<T: ?Sized>(boxed::Box<T>);
|
@@ -128,6 +128,7 @@ impl<T> From<T> for Box<T> {
|
128 | 128 | }
|
129 | 129 | }
|
130 | 130 | impl<T: error::Error> error::Error for Box<T> {
|
| 131 | + #[allow(deprecated)] |
131 | 132 | fn description(&self) -> &str {
|
132 | 133 | error::Error::description(&**self)
|
133 | 134 | }
|
@@ -472,14 +473,14 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Any + Send + 'static> {
|
472 | 473 | /// #[derive(Serialize,Deserialize,Debug)]
|
473 | 474 | /// struct MyError(String);
|
474 | 475 | /// impl fmt::Display for MyError {
|
475 |
| -/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
476 |
| -/// write!(f, "{}", self.0) |
477 |
| -/// } |
| 476 | +/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 477 | +/// write!(f, "{}", self.0) |
| 478 | +/// } |
478 | 479 | /// }
|
479 | 480 | /// impl std::error::Error for MyError {}
|
480 | 481 | ///
|
481 | 482 | /// fn fallible() -> Result<(),s::Box<dyn s::Error>> {
|
482 |
| -/// Err(Box::new(MyError(String::from("boxed error"))) as Box<dyn s::Error>)? |
| 483 | +/// Err(Box::new(MyError(String::from("boxed error"))) as Box<dyn s::Error>)? |
483 | 484 | /// }
|
484 | 485 | ///
|
485 | 486 | /// let serialized = serde_json::to_string(&fallible()).unwrap();
|
@@ -561,7 +562,7 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Error + Send + 'static>
|
561 | 562 | /// extern crate serde_traitobject as s;
|
562 | 563 | ///
|
563 | 564 | /// fn message() -> s::Box<dyn s::Display> {
|
564 |
| -/// s::Box::new(String::from("boxed displayable")) |
| 565 | +/// s::Box::new(String::from("boxed displayable")) |
565 | 566 | /// }
|
566 | 567 | ///
|
567 | 568 | /// let serialized = serde_json::to_string(&message()).unwrap();
|
@@ -630,7 +631,7 @@ impl<'de> serde::de::Deserialize<'de> for boxed::Box<dyn Display + Send + 'stati
|
630 | 631 | /// extern crate serde_traitobject as s;
|
631 | 632 | ///
|
632 | 633 | /// fn debug() -> s::Box<dyn s::Debug> {
|
633 |
| -/// s::Box::new(String::from("boxed debuggable")) |
| 634 | +/// s::Box::new(String::from("boxed debuggable")) |
634 | 635 | /// }
|
635 | 636 | ///
|
636 | 637 | /// let serialized = serde_json::to_string(&debug()).unwrap();
|
@@ -703,6 +704,16 @@ impl<'a, Args, Output> AsRef<Self> for dyn FnOnce<Args, Output = Output> + Send
|
703 | 704 | self
|
704 | 705 | }
|
705 | 706 | }
|
| 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 | +} |
706 | 717 |
|
707 | 718 | impl<Args: 'static, Output: 'static> serde::ser::Serialize for dyn FnOnce<Args, Output = Output> {
|
708 | 719 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
@@ -743,6 +754,50 @@ impl<'de, Args: 'static, Output: 'static> serde::de::Deserialize<'de>
|
743 | 754 | .map(|x| x.0)
|
744 | 755 | }
|
745 | 756 | }
|
| 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 | +} |
746 | 801 |
|
747 | 802 | /// A convenience trait implemented on all (de)serializable implementors of [`std::ops::FnMut`].
|
748 | 803 | ///
|
|
0 commit comments