|
|
|
@ -4,6 +4,8 @@ use std::{
|
|
|
|
|
marker::{
|
|
|
|
|
PhantomData,
|
|
|
|
|
Unpin,
|
|
|
|
|
Send,
|
|
|
|
|
Sync,
|
|
|
|
|
},
|
|
|
|
|
io,
|
|
|
|
|
collections::HashMap,
|
|
|
|
@ -22,10 +24,14 @@ use tokio::{
|
|
|
|
|
AsyncWrite,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
use bytes::{
|
|
|
|
|
IntoBytes,
|
|
|
|
|
FromBytes,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Represents an opaque bytes stream of serialised data to insert into `SuspendStream`.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub struct SuspendObject
|
|
|
|
|
pub struct Object
|
|
|
|
|
{
|
|
|
|
|
data: Vec<u8>,
|
|
|
|
|
data_instances: HashMap<Cow<'static, str>, Range<usize>>,
|
|
|
|
@ -48,9 +54,9 @@ where I: IntoIterator<Item = T>,
|
|
|
|
|
max
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SuspendObject
|
|
|
|
|
impl Object
|
|
|
|
|
{
|
|
|
|
|
/// Create a new empty `SuspendObject`.
|
|
|
|
|
/// Create a new empty `Object`.
|
|
|
|
|
#[inline] pub fn new() -> Self
|
|
|
|
|
{
|
|
|
|
|
Self{data: Vec::new(), data_instances: HashMap::new()}
|
|
|
|
@ -68,6 +74,45 @@ impl SuspendObject
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Insert a boolean
|
|
|
|
|
pub fn insert_bool(&mut self, name: impl Into<Cow<'static, str>>, value: bool)
|
|
|
|
|
{
|
|
|
|
|
self.insert_bytes(name, if value {&[1]} else {&[0]})
|
|
|
|
|
}
|
|
|
|
|
/// Get a boolean
|
|
|
|
|
pub fn get_bool(&self, name: impl Borrow<str>) -> Option<bool>
|
|
|
|
|
{
|
|
|
|
|
self.get_bytes(name).map(|x| if x[0]==0 {false} else {true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Insert a UTF-8 string
|
|
|
|
|
pub fn insert_string(&mut self, name: impl Into<Cow<'static, str>>, stri: impl AsRef<str>)
|
|
|
|
|
{
|
|
|
|
|
self.insert_bytes(name, stri.as_ref().as_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Try to get a UTF-8 string, returning `None` if either the name was not present, or the string was not formatted correctly
|
|
|
|
|
pub fn try_get_string(&self, name: impl Borrow<str>) -> Option<&str>
|
|
|
|
|
{
|
|
|
|
|
if let Some(bytes) = self.get_bytes(name) {
|
|
|
|
|
std::str::from_utf8(bytes).ok()
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// Try to get a UTF-8 string, returning `None` if the name was not present
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// If the present string was not valid UTF-8
|
|
|
|
|
pub fn get_string(&self, name: impl Borrow<str>) -> Option<&str>
|
|
|
|
|
{
|
|
|
|
|
if let Some(bytes) = self.get_bytes(name) {
|
|
|
|
|
Some(std::str::from_utf8(bytes).expect("String contained invalid UTF-8"))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Insert bytes directly with this name
|
|
|
|
|
pub fn insert_bytes(&mut self, name: impl Into<Cow<'static, str>>, bytes: impl AsRef<[u8]>)
|
|
|
|
|
{
|
|
|
|
@ -80,13 +125,32 @@ impl SuspendObject
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Insert a value's bytes directly with this name
|
|
|
|
|
pub unsafe fn insert_value<T,U>(&mut self, name: U, value: &T)
|
|
|
|
|
pub unsafe fn insert_value_raw<T,U>(&mut self, name: U, value: &T)
|
|
|
|
|
where T: ?Sized,
|
|
|
|
|
U: Into<Cow<'static, str>>
|
|
|
|
|
{
|
|
|
|
|
self.insert_bytes(name, bytes::refer(value))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert a value to bytes and insert it into this object
|
|
|
|
|
pub fn insert_value<T: IntoBytes, U>(&mut self, name: U, value: T)
|
|
|
|
|
where U: Into<Cow<'static, str>>
|
|
|
|
|
{
|
|
|
|
|
self.insert_bytes(name, value.into_bytes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Try to get a value from the bytes specified by name
|
|
|
|
|
pub fn get_value<T>(&self, name: impl Borrow<str>) -> Result<Option<T>, T::Error>
|
|
|
|
|
where T: FromBytes
|
|
|
|
|
{
|
|
|
|
|
if let Some(range) = self.data_instances.get(name.borrow()) {
|
|
|
|
|
Ok(Some(T::from_bytes(&self.data[range.clone()])?))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Try to get the bytes specified by name
|
|
|
|
|
pub fn get_bytes(&self, name: impl Borrow<str>) -> Option<&[u8]>
|
|
|
|
|
{
|
|
|
|
@ -101,13 +165,13 @@ impl SuspendObject
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
/// If `T` cannot fit into the size of the range
|
|
|
|
|
pub unsafe fn get_value<T>(&self, name: impl Borrow<str>) -> Option<&T>
|
|
|
|
|
pub unsafe fn get_value_raw<T>(&self, name: impl Borrow<str>) -> Option<&T>
|
|
|
|
|
{
|
|
|
|
|
self.get_bytes(name).map(|x| bytes::derefer(x))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Try to get the value specified by name. Will return `None` if `T` cannot fit in the returned bytes.
|
|
|
|
|
pub unsafe fn try_get_value<T>(&self, name: impl Borrow<str>) -> Option<&T>
|
|
|
|
|
pub unsafe fn try_get_value_raw<T>(&self, name: impl Borrow<str>) -> Option<&T>
|
|
|
|
|
{
|
|
|
|
|
if let Some(bytes) = self.get_bytes(name) {
|
|
|
|
|
if bytes.len() >= std::mem::size_of::<T>() {
|
|
|
|
@ -249,22 +313,22 @@ impl SuspendObject
|
|
|
|
|
pub trait SuspendStream
|
|
|
|
|
{
|
|
|
|
|
/// Write an object into the opaque stream.
|
|
|
|
|
async fn set_object(&mut self, obj: SuspendObject) -> Result<(), SuspendError>;
|
|
|
|
|
async fn set_object(&mut self, obj: Object) -> Result<(), Error>;
|
|
|
|
|
/// Read an object from the opaque stream.
|
|
|
|
|
async fn get_object(&mut self) -> Result<Option<SuspendObject>, SuspendError>;
|
|
|
|
|
async fn get_object(&mut self) -> Result<Option<Object>, Error>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An error that occoured in a suspend operation
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
#[non_exhaustive]
|
|
|
|
|
pub enum SuspendError {
|
|
|
|
|
pub enum Error {
|
|
|
|
|
BadObject,
|
|
|
|
|
Corruption,
|
|
|
|
|
IO(io::Error),
|
|
|
|
|
|
|
|
|
|
Unknown,
|
|
|
|
|
}
|
|
|
|
|
impl error::Error for SuspendError
|
|
|
|
|
impl error::Error for Error
|
|
|
|
|
{
|
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
|
Some(match &self {
|
|
|
|
@ -273,7 +337,7 @@ impl error::Error for SuspendError
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl fmt::Display for SuspendError
|
|
|
|
|
impl fmt::Display for Error
|
|
|
|
|
{
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
{
|
|
|
|
@ -291,14 +355,15 @@ impl fmt::Display for SuspendError
|
|
|
|
|
#[async_trait]
|
|
|
|
|
pub trait Suspendable: Sized
|
|
|
|
|
{
|
|
|
|
|
async fn suspend<S: SuspendStream + ?Sized>(self, into: &mut S) -> Result<(), SuspendError>;
|
|
|
|
|
async fn load<S: SuspendStream + ?Sized>(from: &mut S) -> Result<Self, SuspendError>;
|
|
|
|
|
async fn suspend<S: SuspendStream + Send + Sync+ ?Sized>(self, into: &mut S) -> Result<(), Error>;
|
|
|
|
|
async fn load<S: SuspendStream + Send+ Sync+?Sized>(from: &mut S) -> Result<Self, Error>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
pub struct SuspenceState<T>
|
|
|
|
|
where T: Suspendable
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
|
}*/
|
|
|
|
|