use binrw::binrw;
#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
use super::Bytes;
#[binrw]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "zeroize", derive(Zeroize))]
pub struct MpInt<'b>(Bytes<'b>);
impl<'b> MpInt<'b> {
pub fn from_bytes(bytes: impl Into<Bytes<'b>>) -> Self {
Self(bytes.into())
}
pub fn positive(value: &'b [u8]) -> Self {
match value.first() {
Some(byte) if *byte >= 0x80 => {
let mut buffer = vec![0u8; value.len() + 1];
buffer[1..].copy_from_slice(value);
Self(Bytes::owned(buffer))
}
_ => Self(Bytes::borrowed(value)),
}
}
pub fn as_borrow<'a: 'b>(&'a self) -> MpInt<'a> {
Self(self.0.as_borrow())
}
}
impl AsRef<[u8]> for MpInt<'_> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}