mirror of
https://github.com/extism/extism.git
synced 2026-01-09 13:57:55 -05:00
chore: implement ToBytes/FromBytes for u8 and bool values
This commit is contained in:
@@ -105,6 +105,23 @@ impl FromBytesOwned for String {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromBytesOwned for u8 {
|
||||
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
|
||||
if data.is_empty() {
|
||||
anyhow::bail!("not enough bytes, expected 1, got 0");
|
||||
}
|
||||
|
||||
Ok(data[0])
|
||||
}
|
||||
}
|
||||
|
||||
impl FromBytesOwned for bool {
|
||||
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
|
||||
let x: u8 = u8::from_bytes_owned(data)?;
|
||||
Ok(if x == 0 { false } else { true })
|
||||
}
|
||||
}
|
||||
|
||||
impl FromBytesOwned for f64 {
|
||||
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
|
||||
Ok(Self::from_le_bytes(data.try_into()?))
|
||||
|
||||
@@ -128,6 +128,22 @@ impl<'a> ToBytes<'a> for i32 {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToBytes<'a> for u8 {
|
||||
type Bytes = [u8; 1];
|
||||
|
||||
fn to_bytes(&self) -> Result<Self::Bytes, Error> {
|
||||
Ok([*self])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToBytes<'a> for bool {
|
||||
type Bytes = [u8; 1];
|
||||
|
||||
fn to_bytes(&self) -> Result<Self::Bytes, Error> {
|
||||
Ok([if *self { 1 } else { 0 }])
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToBytes<'a> for u64 {
|
||||
type Bytes = [u8; 8];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user