Skip to content

Commit 95fe005

Browse files
committed
Handle serde/hubpack features better
1 parent 41d3734 commit 95fe005

3 files changed

Lines changed: 14 additions & 44 deletions

File tree

gateway-ereport-messages/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ edition = "2024"
55
license = "MPL-2.0"
66

77
[dependencies]
8+
hubpack = { workspace = true, optional = true }
89
serde = { workspace = true, features = ["derive"], optional = true }
910
zerocopy = { workspace = true, features = ["derive"] }
10-
hubpack = { workspace = true }
1111

1212
[features]
1313
debug-impls = []

gateway-ereport-messages/src/lib.rs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -95,50 +95,20 @@ impl From<Ena> for u64 {
9595
)]
9696
#[repr(transparent)]
9797
#[cfg_attr(any(feature = "debug-impls", test), derive(Debug))]
98+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99+
#[cfg_attr(feature = "serde", serde(from = "u128", into = "u128"))]
98100
pub struct RestartId(pub le::U128);
99101

100-
// Unfortunately, `zerocopy::le::U128` doesn't implement serde traits. Use a
101-
// small decoy type to paper over the differences, without pulling in the
102-
// `serde_with` or `serde_as` crates.
103-
mod serde_jig {
104-
use hubpack::SerializedSize;
105-
use serde::{Deserialize, Serialize};
106-
107-
#[derive(Serialize, Deserialize, SerializedSize)]
108-
pub(crate) struct RestartId(pub(crate) u128);
109-
110-
impl Serialize for super::RestartId {
111-
#[inline]
112-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
113-
where
114-
S: serde::Serializer,
115-
{
116-
let jig = RestartId(self.into_u128());
117-
jig.serialize(serializer)
118-
}
119-
}
120-
121-
impl<'d> Deserialize<'d> for super::RestartId {
122-
#[inline]
123-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
124-
where
125-
D: serde::Deserializer<'d>,
126-
{
127-
let jig = RestartId::deserialize(deserializer)?;
128-
Ok(super::RestartId::new(jig.0))
129-
}
130-
}
131-
132-
impl SerializedSize for super::RestartId {
133-
const MAX_SIZE: usize = const {
134-
assert!(
135-
core::mem::size_of::<super::RestartId>()
136-
== core::mem::size_of::<RestartId>(),
137-
"RestartId changed!",
138-
);
139-
<RestartId as SerializedSize>::MAX_SIZE
140-
};
141-
}
102+
// Unfortunately, `zerocopy::le::U128` doesn't implement hubpack traits.
103+
#[cfg(feature = "hubpack")]
104+
impl hubpack::SerializedSize for RestartId {
105+
const MAX_SIZE: usize = const {
106+
assert!(
107+
core::mem::size_of::<RestartId>() == core::mem::size_of::<u128>(),
108+
"RestartId changed!",
109+
);
110+
<u128 as hubpack::SerializedSize>::MAX_SIZE
111+
};
142112
}
143113

144114
impl RestartId {

gateway-sp-comms/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ zerocopy.workspace = true
3636
zip.workspace = true
3737

3838
gateway-messages = { workspace = true, features = ["std"] }
39-
gateway-ereport-messages = { workspace = true, features = ["debug-impls", "serde"] }
39+
gateway-ereport-messages = { workspace = true, features = ["debug-impls", "serde", "hubpack"] }
4040

4141
# This is required for the build.rs script to check for an appropriate compiler
4242
# version so that `usdt` can be built on stable rust.

0 commit comments

Comments
 (0)