|
|
@ -5,24 +5,24 @@ use std::{error,fmt};
|
|
|
|
|
|
|
|
|
|
|
|
/// An image of the entire post container
|
|
|
|
/// An image of the entire post container
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
|
|
|
pub struct Imouto
|
|
|
|
pub struct Freeze
|
|
|
|
{
|
|
|
|
{
|
|
|
|
posts: Vec<post::Post>,
|
|
|
|
posts: Vec<post::Post>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<Imouto> for Oneesan
|
|
|
|
impl From<Freeze> for Imouto
|
|
|
|
{
|
|
|
|
{
|
|
|
|
#[inline] fn from(from: Imouto) -> Self
|
|
|
|
#[inline] fn from(from: Freeze) -> Self
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Self::from_freeze(from)
|
|
|
|
Self::from_freeze(from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Oneesan> for Imouto
|
|
|
|
impl TryFrom<Imouto> for Freeze
|
|
|
|
{
|
|
|
|
{
|
|
|
|
type Error = FreezeError;
|
|
|
|
type Error = FreezeError;
|
|
|
|
|
|
|
|
|
|
|
|
#[inline] fn try_from(from: Oneesan) -> Result<Self, Self::Error>
|
|
|
|
#[inline] fn try_from(from: Imouto) -> Result<Self, Self::Error>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
from.try_into_freeze()
|
|
|
|
from.try_into_freeze()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -65,16 +65,16 @@ impl fmt::Display for FreezeError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Oneesan
|
|
|
|
impl Imouto
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/// Create a serialisable image of this store by cloning each post into it.
|
|
|
|
/// Create a serialisable image of this store by cloning each post into it.
|
|
|
|
pub async fn freeze(&self) -> Imouto
|
|
|
|
pub async fn freeze(&self) -> Freeze
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let read = self.posts.read().await;
|
|
|
|
let read = &self.all;
|
|
|
|
let mut sis = Imouto{
|
|
|
|
let mut sis = Freeze{
|
|
|
|
posts: Vec::with_capacity(read.0.len()),
|
|
|
|
posts: Vec::with_capacity(read.len()),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
for (_, post) in read.0.iter()
|
|
|
|
for (_, post) in read.iter()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sis.posts.push(post.read().await.clone());
|
|
|
|
sis.posts.push(post.read().await.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -86,13 +86,13 @@ impl Oneesan
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Fails
|
|
|
|
/// # Fails
|
|
|
|
/// If references to any posts are still held elsewhere.
|
|
|
|
/// If references to any posts are still held elsewhere.
|
|
|
|
pub fn try_into_freeze(self) -> Result<Imouto, FreezeError>
|
|
|
|
pub fn try_into_freeze(self) -> Result<Freeze, FreezeError>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let read = self.posts.into_inner();
|
|
|
|
let read = self.all;
|
|
|
|
let mut sis = Imouto{
|
|
|
|
let mut sis = Freeze{
|
|
|
|
posts: Vec::with_capacity(read.0.len()),
|
|
|
|
posts: Vec::with_capacity(read.len()),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
for post in read.0.into_iter()
|
|
|
|
for post in read.into_iter()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
sis.posts.push(match Arc::try_unwrap(post) {
|
|
|
|
sis.posts.push(match Arc::try_unwrap(post) {
|
|
|
|
Ok(val) => val.into_inner(),
|
|
|
|
Ok(val) => val.into_inner(),
|
|
|
@ -108,13 +108,13 @@ impl Oneesan
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
/// # Panics
|
|
|
|
/// If references to any posts are still held elsewhere.
|
|
|
|
/// If references to any posts are still held elsewhere.
|
|
|
|
pub fn into_freeze(self) -> Imouto
|
|
|
|
pub fn into_freeze(self) -> Freeze
|
|
|
|
{
|
|
|
|
{
|
|
|
|
self.try_into_freeze().expect("Failed to consume into freeze")
|
|
|
|
self.try_into_freeze().expect("Failed to consume into freeze")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new store from a serialisable image of one by cloning each post in it
|
|
|
|
/// Create a new store from a serialisable image of one by cloning each post in it
|
|
|
|
pub fn unfreeze(freeze: &Imouto) -> Self
|
|
|
|
pub fn unfreeze(freeze: &Freeze) -> Self
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let mut posts = Arena::new();
|
|
|
|
let mut posts = Arena::new();
|
|
|
|
let mut user_map = HashMap::new();
|
|
|
|
let mut user_map = HashMap::new();
|
|
|
@ -128,12 +128,13 @@ impl Oneesan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
posts: RwLock::new((posts, user_map))
|
|
|
|
all: posts,
|
|
|
|
|
|
|
|
user_map,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new store by consuming serialisable image of one by cloning each post in it
|
|
|
|
/// Create a new store by consuming serialisable image of one by cloning each post in it
|
|
|
|
pub fn from_freeze(freeze: Imouto) -> Self
|
|
|
|
pub fn from_freeze(freeze: Freeze) -> Self
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let mut posts = Arena::new();
|
|
|
|
let mut posts = Arena::new();
|
|
|
|
let mut user_map = HashMap::new();
|
|
|
|
let mut user_map = HashMap::new();
|
|
|
@ -147,7 +148,8 @@ impl Oneesan
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
posts: RwLock::new((posts, user_map))
|
|
|
|
all: posts,
|
|
|
|
|
|
|
|
user_map,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|