Set up IdSet
This commit is contained in:
32
Cargo.lock
generated
32
Cargo.lock
generated
@@ -1698,7 +1698,7 @@ dependencies = [
|
||||
"gloo-console 0.4.0",
|
||||
"gloo-timers 0.4.0",
|
||||
"implicit-clone",
|
||||
"luminescent-dreams-common",
|
||||
"luminescent-dreams-common 0.1.0",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.19",
|
||||
@@ -2812,6 +2812,28 @@ dependencies = [
|
||||
"tokio-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "luminescent-dreams-common"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5426c81c7c05f72534e06ae7e395c47451ccc6bff0021b6db4a1236deef3285"
|
||||
dependencies = [
|
||||
"luminescent-dreams-common-derive",
|
||||
"nonempty",
|
||||
"tokio",
|
||||
"tokio-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "luminescent-dreams-common-derive"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "932c6d54c3029e6e8e6d52574944ca0e57190ad95ff22db89c8d124a0f75d2c8"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"syn 3.0.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "markdown"
|
||||
version = "1.0.0"
|
||||
@@ -2913,6 +2935,12 @@ dependencies = [
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nonempty"
|
||||
version = "0.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9737e026353e5cd0736f98eddae28665118eb6f6600902a7f50db585621fecb6"
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.50.3"
|
||||
@@ -5047,7 +5075,7 @@ dependencies = [
|
||||
"implicit-clone",
|
||||
"jiff",
|
||||
"lazy_static",
|
||||
"luminescent-dreams-common",
|
||||
"luminescent-dreams-common 0.2.0",
|
||||
"markdown",
|
||||
"password-strength",
|
||||
"serde",
|
||||
|
||||
@@ -67,7 +67,7 @@ http = { version = "1.3" }
|
||||
implicit-clone = { version = "0.6" }
|
||||
jiff = { version = "0.2.18" }
|
||||
lazy_static = { version = "1.5" }
|
||||
luminescent-dreams-common = { version = "0.1" }
|
||||
luminescent-dreams-common = { version = "0.2" }
|
||||
markdown = { version = "1.0" }
|
||||
password-strength = { version = "1" }
|
||||
serde = { version = "1.0", features = ["derive", "serde_derive"] }
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use implicit_clone::{ImplicitClone, unsync::IArray};
|
||||
use yew::prelude::*;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
@@ -49,3 +50,65 @@ pub fn AuthenticationOptionElement(
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/// An `IdSet` contains a bunch of items whose uniqueness is dictated by the Identifiable trait.
|
||||
/// Internally, the `IdSet` contains an `IArray`, which makes cloning it cheap.
|
||||
#[derive(Clone, ImplicitClone, PartialEq)]
|
||||
pub struct IdSet<T: luminescent_dreams_common::Identifiable + ImplicitClone + 'static>(IArray<T>);
|
||||
|
||||
impl<T: luminescent_dreams_common::Identifiable + ImplicitClone + 'static> IdSet<T> {
|
||||
pub fn get(&self, id: &T::Id) -> Option<&T> {
|
||||
self.0.iter().find(|i| i.id() == *id)
|
||||
}
|
||||
|
||||
pub fn insert(self, item: T) -> Self {
|
||||
let item_id = item.id();
|
||||
IdSet(
|
||||
self.0
|
||||
.into_iter()
|
||||
.filter(|i| i.id() != item_id)
|
||||
.chain([item])
|
||||
.collect::<Vec<T>>()
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn remove(self, id: T::Id) -> Self {
|
||||
let item_id = id;
|
||||
IdSet(
|
||||
self.0
|
||||
.into_iter()
|
||||
.filter(|i| i.id() != item_id)
|
||||
.collect::<Vec<T>>()
|
||||
.into(),
|
||||
)
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn iter(&self) -> std::iter::Iter<Item = &T> {
|
||||
todo!()
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl<T: luminescent_dreams_common::Identifiable + ImplicitClone + 'static> Default for IdSet<T> {
|
||||
fn default() -> Self {
|
||||
Self(IArray::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: luminescent_dreams_common::Identifiable + ImplicitClone + 'static> From<IdSet<T>>
|
||||
for IArray<T>
|
||||
{
|
||||
fn from(set: IdSet<T>) -> Self {
|
||||
set.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: luminescent_dreams_common::Identifiable + ImplicitClone + 'static> From<Vec<T>>
|
||||
for IdSet<T>
|
||||
{
|
||||
fn from(lst: Vec<T>) -> Self {
|
||||
Self(lst.into())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user