Skip to main content

klend_interface/state/
pod.rs

1//! Minimal zero-copy integer wrapper.
2//!
3//! Vendored from [`spl_pod::primitives::PodU128`] (byte-for-byte layout compatible) so that
4//! `solana-zk-sdk` — which `spl-pod >= 0.4` pulls in unconditionally, and which requires
5//! Rust >= 1.82 (`core::iter::repeat_n`) — stays out of this crate's dependency closure.
6//! Keeping it out is what lets the published MSRV be 1.81.
7//!
8//! [`spl_pod::primitives::PodU128`]: https://docs.rs/spl-pod/latest/spl_pod/primitives/struct.PodU128.html
9
10use bytemuck::{Pod, Zeroable};
11
12/// A `u128` stored as little-endian bytes, so it can be a field inside `#[repr(C)]` `Pod` structs.
13#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
14#[repr(transparent)]
15pub struct PodU128(pub [u8; 16]);
16
17impl PodU128 {
18    /// Build from a native `u128` (little-endian); usable in `const` context.
19    pub const fn from_primitive(n: u128) -> Self {
20        Self(n.to_le_bytes())
21    }
22}
23
24impl From<u128> for PodU128 {
25    fn from(n: u128) -> Self {
26        Self::from_primitive(n)
27    }
28}
29
30impl From<PodU128> for u128 {
31    fn from(pod: PodU128) -> Self {
32        u128::from_le_bytes(pod.0)
33    }
34}