Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 8593a5b

Browse files
authored
[libraries/pod]: add PodU128 type (#7012)
Add PodU128 type
1 parent a898de1 commit 8593a5b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

libraries/pod/src/primitives.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ impl_int_conversion!(PodU64, u64);
115115
pub struct PodI64([u8; 8]);
116116
impl_int_conversion!(PodI64, i64);
117117

118+
/// `u128` type that can be used in Pods
119+
#[cfg_attr(
120+
feature = "borsh",
121+
derive(BorshDeserialize, BorshSerialize, BorshSchema)
122+
)]
123+
#[cfg_attr(feature = "serde-traits", derive(Serialize, Deserialize))]
124+
#[cfg_attr(feature = "serde-traits", serde(from = "u128", into = "u128"))]
125+
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
126+
#[repr(transparent)]
127+
pub struct PodU128(pub [u8; 16]);
128+
impl_int_conversion!(PodU128, u128);
129+
118130
#[cfg(test)]
119131
mod tests {
120132
use {super::*, crate::bytemuck::pod_from_bytes};
@@ -230,4 +242,28 @@ mod tests {
230242
let deserialized = serde_json::from_str::<PodI64>(&serialized).unwrap();
231243
assert_eq!(pod_i64, deserialized);
232244
}
245+
246+
#[test]
247+
fn test_pod_u128() {
248+
assert!(pod_from_bytes::<PodU128>(&[]).is_err());
249+
assert_eq!(
250+
1u128,
251+
u128::from(
252+
*pod_from_bytes::<PodU128>(&[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
253+
.unwrap()
254+
)
255+
);
256+
}
257+
258+
#[cfg(feature = "serde-traits")]
259+
#[test]
260+
fn test_pod_u128_serde() {
261+
let pod_u128: PodU128 = u128::MAX.into();
262+
263+
let serialized = serde_json::to_string(&pod_u128).unwrap();
264+
assert_eq!(&serialized, "340282366920938463463374607431768211455");
265+
266+
let deserialized = serde_json::from_str::<PodU128>(&serialized).unwrap();
267+
assert_eq!(pod_u128, deserialized);
268+
}
233269
}

0 commit comments

Comments
 (0)