Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Nov 6, 2024
1 parent 53a75de commit 1847375
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion utils/zerovec/src/ule/tuplevar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ macro_rules! tuple_varule {
}

#[cfg(feature = "serde")]
impl<'de, $($T: VarULE + ?Sized),+> serde::Deserialize<'de> for Box<$name<$($T),+>>
impl<'de, $($T: VarULE + ?Sized),+> serde::Deserialize<'de> for Box<$name<$($T),+>>
where
// This impl should be present on almost all deserializable VarULE types
$( Box<$T>: serde::Deserialize<'de>),+ {
Expand Down
56 changes: 56 additions & 0 deletions utils/zerovec/src/ule/vartuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
//! assert_eq!(&employees_vzv.get(1).unwrap().variable, "John Doe");
//! ```
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use core::mem::{size_of, transmute_copy};
use zerofrom::ZeroFrom;

use super::{AsULE, EncodeAsVarULE, UleError, VarULE, ULE};

Expand All @@ -58,6 +61,7 @@ use super::{AsULE, EncodeAsVarULE, UleError, VarULE, ULE};
/// See the module for examples.
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)] // well-defined type
#[derive(serde::Serialize)]
pub struct VarTuple<A, B> {
pub sized: A,
pub variable: B,
Expand Down Expand Up @@ -180,6 +184,58 @@ where
}
}

impl<A, V> ToOwned for VarTupleULE<A, V>
where
A: AsULE + 'static,
V: VarULE + ?Sized,
{
type Owned = Box<Self>;
fn to_owned(&self) -> Self::Owned {
crate::ule::encode_varule_to_box(self)
}
}

impl<'a, A, B, V> ZeroFrom<'a, VarTupleULE<A, V>> for VarTuple<A, B>
where
A: AsULE + 'static,
V: VarULE + ?Sized,
B: ZeroFrom<'a, V>,
{
fn zero_from(other: &'a VarTupleULE<A, V>) -> Self {
VarTuple {
sized: AsULE::from_unaligned(other.sized),
variable: B::zero_from(&other.variable),
}
}
}

#[cfg(feature = "serde")]
impl<A, V> serde::Serialize for VarTupleULE<A, V>
where
A: AsULE + 'static,
V: VarULE + ?Sized,
for<'a> &'a V: ZeroFrom<'a, V>,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
let this = <($(&$T),+)>::zero_from(self);
<($(&$T),+) as serde::Serialize>::serialize(&this, serializer)
}
}

#[cfg(feature = "serde")]
impl<'de, $($T: VarULE + ?Sized),+> serde::Deserialize<'de> for Box<$name<$($T),+>>
where
// This impl should be present on almost all deserializable VarULE types
$( Box<$T>: serde::Deserialize<'de>),+ {
fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error> where Des: serde::Deserializer<'de> {
let this = <( $(Box<$T>),+) as serde::Deserialize>::deserialize(deserializer)?;
let this_ref = (
$(&*this.$i),+
);
Ok(crate::ule::encode_varule_to_box(&this_ref))
}
}

#[test]
fn test_simple() {
let var_tuple = VarTuple {
Expand Down

0 comments on commit 1847375

Please sign in to comment.