Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serde_ref and serde_ref_mut utility modules #481

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/tests/manual_leak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct WrappedKey<'key> {
#[serde(
serialize_with = "test_fuzz::serialize_ref",
deserialize_with = "test_fuzz::deserialize_ref"
)]
#[serde(with = "test_fuzz::serde_ref")]
key: &'key [u8; 32],
}

Expand Down
5 changes: 4 additions & 1 deletion test-fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ pub use cast_checks;
pub use internal::serde_format;

mod utils;
pub use utils::{deserialize_ref, deserialize_ref_mut, serialize_ref, serialize_ref_mut};
pub use utils::{
deserialize_ref, deserialize_ref_mut, serde_ref, serde_ref_mut, serialize_ref,
serialize_ref_mut,
};

mod convert;
pub use convert::{FromRef, Into};
10 changes: 10 additions & 0 deletions test-fuzz/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ macro_rules! leak {
};
}

pub mod serde_ref {
pub use super::deserialize_ref as deserialize;
pub use super::serialize_ref as serialize;
}

/// `serialize_ref` functions similar to `leak!`, but it is meant to be used with Serde's
/// [`serialize_with`](https://serde.rs/field-attrs.html#serialize_with) field attribute.
#[inline]
Expand All @@ -74,6 +79,11 @@ where
Ok(Box::leak(Box::new(x)))
}

pub mod serde_ref_mut {
pub use super::deserialize_ref_mut as deserialize;
pub use super::serialize_ref_mut as serialize;
}

/// `serialize_ref_mut` is similar to `serialize_ref`, except it operates on a mutable reference
/// instead of an immutable one.
pub fn serialize_ref_mut<S, T>(x: &&mut T, serializer: S) -> Result<S::Ok, S::Error>
Expand Down