Skip to content

Commit

Permalink
fix(boc): allow unsized boc-related types to serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Oct 19, 2024
1 parent b65137e commit 20dc8b6
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/boc/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<T> From<T> for SerdeBoc<T> {
}
}

impl<T: SerializeAsBoc> Serialize for SerdeBoc<T> {
impl<T: SerializeAsBoc + ?Sized> Serialize for SerdeBoc<T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.serialize_as_boc(serializer)
}
Expand Down Expand Up @@ -83,7 +83,7 @@ impl SerializeAsBoc for DynCell {
}
}

impl<T: SerializeAsBoc> SerializeAsBoc for &'_ T {
impl<T: SerializeAsBoc + ?Sized> SerializeAsBoc for &'_ T {
#[inline]
fn serialize_as_boc<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
T::serialize_as_boc(self, serializer)
Expand All @@ -97,21 +97,21 @@ impl<T: SerializeAsBoc> SerializeAsBoc for Option<T> {
}
}

impl<T: SerializeAsBoc> SerializeAsBoc for Box<T> {
impl<T: SerializeAsBoc + ?Sized> SerializeAsBoc for Box<T> {
#[inline]
fn serialize_as_boc<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
T::serialize_as_boc(self.as_ref(), serializer)
}
}

impl<T: SerializeAsBoc> SerializeAsBoc for Rc<T> {
impl<T: SerializeAsBoc + ?Sized> SerializeAsBoc for Rc<T> {
#[inline]
fn serialize_as_boc<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
T::serialize_as_boc(self.as_ref(), serializer)
}
}

impl<T: SerializeAsBoc> SerializeAsBoc for Arc<T> {
impl<T: SerializeAsBoc + ?Sized> SerializeAsBoc for Arc<T> {
#[inline]
fn serialize_as_boc<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
T::serialize_as_boc(self.as_ref(), serializer)
Expand Down Expand Up @@ -395,3 +395,24 @@ where

deserializer.deserialize_bytes(CowBytesVisitor)
}

#[cfg(test)]
mod tests {
use crate::cell::CellFamily;

use super::*;

#[derive(serde::Serialize)]
struct StructWithDynCell<'a> {
#[serde(with = "crate::boc::Boc")]
cell: &'a DynCell,
}

#[test]
fn serde_dyn_cell_works() {
serde_json::to_string(&StructWithDynCell {
cell: Cell::empty_cell_ref(),
})
.unwrap();
}
}

0 comments on commit 20dc8b6

Please sign in to comment.