freeze: create, from

new-idea
Avril 4 years ago
parent 31ecef8954
commit 0a6c1304dc
Signed by: flanchan
GPG Key ID: 284488987C31F630

44
Cargo.lock generated

@ -362,9 +362,9 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]]
name = "futures"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b3b0c040a1fe6529d30b3c5944b280c7f0dcb2930d2c3062bca967b602583d0"
checksum = "da9052a1a50244d8d5aa9bf55cbc2fb6f357c86cc52e46c62ed390a7180cf150"
dependencies = [
"futures-channel",
"futures-core",
@ -377,9 +377,9 @@ dependencies = [
[[package]]
name = "futures-channel"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b7109687aa4e177ef6fe84553af6280ef2778bdb7783ba44c9dc3399110fe64"
checksum = "f2d31b7ec7efab6eefc7c57233bb10b847986139d88cc2f5a02a1ae6871a1846"
dependencies = [
"futures-core",
"futures-sink",
@ -387,15 +387,15 @@ dependencies = [
[[package]]
name = "futures-core"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "847ce131b72ffb13b6109a221da9ad97a64cbe48feb1028356b836b47b8f1748"
checksum = "79e5145dde8da7d1b3892dad07a9c98fc04bc39892b1ecc9692cf53e2b780a65"
[[package]]
name = "futures-executor"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4caa2b2b68b880003057c1dd49f1ed937e38f22fcf6c212188a121f08cf40a65"
checksum = "e9e59fdc009a4b3096bf94f740a0f2424c082521f20a9b08c5c07c48d90fd9b9"
dependencies = [
"futures-core",
"futures-task",
@ -404,15 +404,15 @@ dependencies = [
[[package]]
name = "futures-io"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "611834ce18aaa1bd13c4b374f5d653e1027cf99b6b502584ff8c9a64413b30bb"
checksum = "28be053525281ad8259d47e4de5de657b25e7bac113458555bb4b70bc6870500"
[[package]]
name = "futures-macro"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77408a692f1f97bcc61dc001d752e00643408fbc922e4d634c655df50d595556"
checksum = "c287d25add322d9f9abdcdc5927ca398917996600182178774032e9f8258fedd"
dependencies = [
"proc-macro-hack",
"proc-macro2",
@ -422,24 +422,24 @@ dependencies = [
[[package]]
name = "futures-sink"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f878195a49cee50e006b02b93cf7e0a95a38ac7b776b4c4d9cc1207cd20fcb3d"
checksum = "caf5c69029bda2e743fddd0582d1083951d65cc9539aebf8812f36c3491342d6"
[[package]]
name = "futures-task"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c554eb5bf48b2426c4771ab68c6b14468b6e76cc90996f528c3338d761a4d0d"
checksum = "13de07eb8ea81ae445aca7b69f5f7bf15d7bf4912d8ca37d6645c77ae8a58d86"
dependencies = [
"once_cell",
]
[[package]]
name = "futures-util"
version = "0.3.8"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d304cff4a7b99cfb7986f7d43fbe93d175e72e704a8860787cc95e9ffd85cbd2"
checksum = "632a8cd0f2a4b3fdea1657f08bde063848c3bd00f9bbf6e256b8be78802e624b"
dependencies = [
"futures-channel",
"futures-core",
@ -448,7 +448,7 @@ dependencies = [
"futures-sink",
"futures-task",
"memchr",
"pin-project 1.0.2",
"pin-project-lite 0.2.4",
"pin-utils",
"proc-macro-hack",
"proc-macro-nested",
@ -1098,9 +1098,9 @@ checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b"
[[package]]
name = "pin-project-lite"
version = "0.2.0"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c"
checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827"
[[package]]
name = "pin-utils"
@ -1712,7 +1712,7 @@ checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3"
dependencies = [
"cfg-if 1.0.0",
"log",
"pin-project-lite 0.2.0",
"pin-project-lite 0.2.4",
"tracing-core",
]

@ -19,7 +19,7 @@ chrono = {version = "0.4", features=["serde"]}
color-eyre = {version = "0.5.10", default-features=false}
cryptohelpers = {version = "1.7.2", features=["full"]}
difference = "2.0.0"
futures = "0.3.8"
futures = "0.3.12"
generational-arena = "0.2.8"
getrandom = "0.2.1"
hex-literal = "0.3.1"

@ -28,6 +28,7 @@ use color_eyre::{
#[macro_use] mod ext; use ext::*;
mod service;
mod bytes;
mod delta; //unused now, but tests still use it, so...
mod hard_format;

@ -1,2 +1 @@
use super::*;
//! Services

@ -1,14 +0,0 @@
use super::*;
use std::collections::{HashMap, HashSet};
use super::*;
use user::{User, UserID};
use post::Post;
/// Contains all posts
#[derive(Debug)]
pub struct State
{
users: HashSet<User>,
posts: HashMap<UserID, MaybeVec<Post>>
}

@ -0,0 +1,91 @@
//! Frozen, serialisable state
use super::*;
use futures::prelude::*;
/// An immutable image of `State`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Freeze
{
users: HashSet<User>,
posts: Vec<(UserID, Post)>,
}
impl Freeze
{
/// Create a `State` from this freeze
///
/// # Notes
/// This clones all post and user data, to perform this operation without cloning, use `into_state`.
pub fn unfreeze(&self) -> State
{
let users: HashMap<UserID, RwLock<User>> = self.users
.iter()
.map(|x| (*x.id(), RwLock::new(x.clone()))).collect();
let mut posts: HashMap<UserID, MaybeVec<RwLock<Post>>> = HashMap::with_capacity(users.len());
for (id, post) in self.posts.iter()
{
posts.entry(*id).or_insert_with(move || MaybeVec::new()).push(RwLock::new(post.clone()));
}
State(Arc::new(Inner{posts: RwLock::new( Posts {
users,
posts
})}))
}
/// Consume into a new `State`.
pub fn into_state(self) -> State
{
State::from_freeze(self)
}
}
impl State
{
/// Create a serialisable image from this state
pub async fn freeze(&self) -> Freeze
{
let posts = self.0.posts.read().await;
let users: HashSet<User> = {
stream::iter(posts.users.iter())
.then(|(_, x)| async move { x.read().await })
.map(|x| x.clone()).collect().await
};
let posts: Vec<_> = {
stream::iter(posts.posts.iter()
.map(|(x, y)| y.into_iter().map(move |z| (x, z))).flatten())
.then(|x| async move { (x.0, x.1.read().await) })
.map(|(&id, post)| (id, post.clone())).collect().await
};
Freeze {users, posts}
}
/// Create `State` from this image
pub fn from_freeze(freeze: Freeze) -> Self
{
let users: HashMap<UserID, RwLock<User>> = freeze.users
.into_iter()
.map(|x| (*x.id(), RwLock::new(x))).collect();
let mut posts: HashMap<UserID, MaybeVec<RwLock<Post>>> = HashMap::with_capacity(users.len());
for (id, post) in freeze.posts.into_iter()
{
posts.entry(id).or_insert_with(move || MaybeVec::new()).push(RwLock::new(post));
}
Self(Arc::new(Inner{posts: RwLock::new( Posts {
users,
posts
})}))
}
}
impl From<Freeze> for State
{
#[inline] fn from(from: Freeze) -> Self
{
Self::from_freeze(from)
}
}

@ -0,0 +1,27 @@
use super::*;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use user::{User, UserID};
use post::Post;
mod freeze;
pub use freeze::*;
#[derive(Debug)]
struct Posts
{
users: HashMap<UserID, RwLock<User>>,
posts: HashMap<UserID, MaybeVec<RwLock<Post>>>,
}
#[derive(Debug)]
struct Inner
{
posts: RwLock<Posts>,
}
/// Contains all posts and users
#[derive(Debug, Clone)]
pub struct State(Arc<Inner>);

@ -52,7 +52,7 @@ impl UserID
///
/// # Hash
/// This type hashes to its unique ID, and also borrows to its unique ID.
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct User
{
/// The user's unique ID.
@ -120,4 +120,10 @@ impl User
}
Ok(false)
}
/// This user's unique ID
pub fn id(&self) -> &UserID
{
&self.id
}
}

Loading…
Cancel
Save