use crate::__private::maybestd::vec::Vec;
use crate::from_slice;
use crate::io::{Error, ErrorKind, Result};
use crate::schema::{BorshSchemaContainer, SchemaMaxSerializedSizeError};
use crate::{BorshDeserialize, BorshSchema, BorshSerialize};
pub fn try_from_slice_with_schema<T: BorshDeserialize + BorshSchema>(v: &[u8]) -> Result<T> {
let (schema, object) = from_slice::<(BorshSchemaContainer, T)>(v)?;
if schema_container_of::<T>() != schema {
return Err(Error::new(
ErrorKind::InvalidData,
"Borsh schema does not match",
));
}
Ok(object)
}
pub fn try_to_vec_with_schema<T: BorshSerialize + BorshSchema + ?Sized>(
value: &T,
) -> Result<Vec<u8>> {
let schema = schema_container_of::<T>();
let mut res = crate::to_vec(&schema)?;
value.serialize(&mut res)?;
Ok(res)
}
pub fn schema_container_of<T: BorshSchema + ?Sized>() -> BorshSchemaContainer {
BorshSchemaContainer::for_type::<T>()
}
pub fn max_serialized_size<T: BorshSchema + ?Sized>(
) -> core::result::Result<usize, SchemaMaxSerializedSizeError> {
let schema = BorshSchemaContainer::for_type::<T>();
schema.max_serialized_size()
}