pub trait FromByteswhere
Self: Sized,{
// Required methods
fn from_bytes(bytes: &[u8]) -> Result<Self>;
fn from_bytes_vec(bytes: Vec<u8>) -> Result<Self>;
}Expand description
A trait to convert Bytes into a specific type.
This trait is implemented for various types such as Vec<u8>, String, &str, and primitive types like u8, i32, etc.
Additionally it supports an JSON deserializable type via the serde and serde_json crates.
Required Methods§
Sourcefn from_bytes(bytes: &[u8]) -> Result<Self>
fn from_bytes(bytes: &[u8]) -> Result<Self>
A way to convert the Bytes into the implementing type without consuming the original bytes.
§Errors
There are several reasons why this conversion might fail returning an errors::SDKError:
errors::SDKError::FromUtf8Errorif the bytes are not valid UTF-8.errors::SDKError::NumBytesConversionif the bytes are not of the expected length for the type of number primitive.errors::SDKError::InvalidValueif the bytes do not represent a valid value for the type.errors::SDKError::Serdeif the bytes cannot be deserialized into the type.
§Examples
use seda_sdk_rs::bytes::{FromBytes, ToBytes};
let bytes = "Hello, world!".to_bytes();
let string: String = String::from_bytes(&bytes).expect("Should be valid UTF-8");
assert_eq!(string, "Hello, world!");Sourcefn from_bytes_vec(bytes: Vec<u8>) -> Result<Self>
fn from_bytes_vec(bytes: Vec<u8>) -> Result<Self>
A way to convert the Bytes into the implementing type and consume the original bytes.
§Errors
There are several reasons why this conversion might fail returning an errors::SDKError:
errors::SDKError::FromUtf8Errorif the bytes are not valid UTF-8.errors::SDKError::NumBytesConversionif the bytes are not of the expected length for the type of number primitive.errors::SDKError::InvalidValueif the bytes do not represent a valid value for the type.errors::SDKError::Serdeif the bytes cannot be deserialized into the type.
§Examples
use seda_sdk_rs::bytes::{FromBytes, ToBytes};
let bytes = "Hello, world!".to_bytes();
let string: String = String::from_bytes_vec(bytes.eject()).expect("Should be valid UTF-8");
assert_eq!(string, "Hello, world!");Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.