1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
use std::{
collections::{HashMap, HashSet},
ops::{Deref, DerefMut, IndexMut},
};
use generational_box::{AnyStorage, UnsyncStorage};
use crate::{ext_methods, read::Readable, read::ReadableExt, MappedMutSignal, WriteSignal};
/// A reference to a value that can be written to.
#[allow(type_alias_bounds)]
pub type WritableRef<'a, T: Writable, O = <T as Readable>::Target> =
WriteLock<'a, O, <T as Readable>::Storage, <T as Writable>::WriteMetadata>;
/// A trait for states that can be written to like [`crate::Signal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API.
///
/// # Example
/// ```rust
/// # use dioxus::prelude::*;
/// enum MyEnum {
/// String(String),
/// Number(i32),
/// }
///
/// fn MyComponent(mut count: Signal<MyEnum>) -> Element {
/// rsx! {
/// button {
/// onclick: move |_| {
/// // You can use any methods from the Writable trait on Signals
/// match &mut *count.write() {
/// MyEnum::String(s) => s.push('a'),
/// MyEnum::Number(n) => *n += 1,
/// }
/// },
/// "Add value"
/// }
/// }
/// }
/// ```
pub trait Writable: Readable {
/// Additional data associated with the write reference.
type WriteMetadata;
/// Try to get a mutable reference to the value without checking the lifetime. This will update any subscribers.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
fn try_write_unchecked(
&self,
) -> Result<WritableRef<'static, Self>, generational_box::BorrowMutError>
where
Self::Target: 'static;
}
/// A mutable reference to a writable value. This reference acts similarly to [`std::cell::RefMut`], but it has extra debug information
/// and integrates with the reactive system to automatically update dependents.
///
/// [`WriteLock`] implements [`DerefMut`] which means you can call methods on the inner value just like you would on a mutable reference
/// to the inner value. If you need to get the inner reference directly, you can call [`WriteLock::deref_mut`].
///
/// # Example
/// ```rust
/// # use dioxus::prelude::*;
/// fn app() -> Element {
/// let mut value = use_signal(|| String::from("hello"));
///
/// rsx! {
/// button {
/// onclick: move |_| {
/// let mut mutable_reference = value.write();
///
/// // You call methods like `push_str` on the reference just like you would with the inner String
/// mutable_reference.push_str("world");
/// },
/// "Click to add world to the string"
/// }
/// div { "{value}" }
/// }
/// }
/// ```
///
/// ## Matching on WriteLock
///
/// You need to get the inner mutable reference with [`WriteLock::deref_mut`] before you match the inner value. If you try to match
/// without calling [`WriteLock::deref_mut`], you will get an error like this:
///
/// ```compile_fail
/// # use dioxus::prelude::*;
/// #[derive(Debug)]
/// enum Colors {
/// Red(u32),
/// Green
/// }
/// fn app() -> Element {
/// let mut value = use_signal(|| Colors::Red(0));
///
/// rsx! {
/// button {
/// onclick: move |_| {
/// let mut mutable_reference = value.write();
///
/// match mutable_reference {
/// // Since we are matching on the `Write` type instead of &mut Colors, we can't match on the enum directly
/// Colors::Red(brightness) => *brightness += 1,
/// Colors::Green => {}
/// }
/// },
/// "Click to add brightness to the red color"
/// }
/// div { "{value:?}" }
/// }
/// }
/// ```
///
/// ```text
/// error[E0308]: mismatched types
/// --> src/main.rs:18:21
/// |
/// 16 | match mutable_reference {
/// | ----------------- this expression has type `dioxus::prelude::Write<'_, Colors>`
/// 17 | // Since we are matching on the `Write` t...
/// 18 | Colors::Red(brightness) => *brightness += 1,
/// | ^^^^^^^^^^^^^^^^^^^^^^^ expected `Write<'_, Colors>`, found `Colors`
/// |
/// = note: expected struct `dioxus::prelude::Write<'_, Colors, >`
/// found enum `Colors`
/// ```
///
/// Instead, you need to call deref mut on the reference to get the inner value **before** you match on it:
///
/// ```rust
/// use std::ops::DerefMut;
/// # use dioxus::prelude::*;
/// #[derive(Debug)]
/// enum Colors {
/// Red(u32),
/// Green
/// }
/// fn app() -> Element {
/// let mut value = use_signal(|| Colors::Red(0));
///
/// rsx! {
/// button {
/// onclick: move |_| {
/// let mut mutable_reference = value.write();
///
/// // DerefMut converts the `Write` into a `&mut Colors`
/// match mutable_reference.deref_mut() {
/// // Now we can match on the inner value
/// Colors::Red(brightness) => *brightness += 1,
/// Colors::Green => {}
/// }
/// },
/// "Click to add brightness to the red color"
/// }
/// div { "{value:?}" }
/// }
/// }
/// ```
///
/// ## Generics
/// - T is the current type of the write
/// - S is the storage type of the signal. This type determines if the signal is local to the current thread, or it can be shared across threads.
/// - D is the additional data associated with the write reference. This is used by signals to track when the write is dropped
pub struct WriteLock<'a, T: ?Sized + 'a, S: AnyStorage = UnsyncStorage, D = ()> {
write: S::Mut<'a, T>,
data: D,
}
impl<'a, T: ?Sized, S: AnyStorage> WriteLock<'a, T, S> {
/// Create a new write reference
pub fn new(write: S::Mut<'a, T>) -> Self {
Self { write, data: () }
}
}
impl<'a, T: ?Sized, S: AnyStorage, D> WriteLock<'a, T, S, D> {
/// Create a new write reference with additional data.
pub fn new_with_metadata(write: S::Mut<'a, T>, data: D) -> Self {
Self { write, data }
}
/// Get the inner value of the write reference.
pub fn into_inner(self) -> S::Mut<'a, T> {
self.write
}
/// Get the additional data associated with the write reference.
pub fn data(&self) -> &D {
&self.data
}
/// Split into the inner value and the additional data.
pub fn into_parts(self) -> (S::Mut<'a, T>, D) {
(self.write, self.data)
}
/// Map the metadata of the write reference to a new type.
pub fn map_metadata<O>(self, f: impl FnOnce(D) -> O) -> WriteLock<'a, T, S, O> {
WriteLock {
write: self.write,
data: f(self.data),
}
}
/// Map the mutable reference to the signal's value to a new type.
pub fn map<O: ?Sized>(
myself: Self,
f: impl FnOnce(&mut T) -> &mut O,
) -> WriteLock<'a, O, S, D> {
let Self { write, data, .. } = myself;
WriteLock {
write: S::map_mut(write, f),
data,
}
}
/// Try to map the mutable reference to the signal's value to a new type
pub fn filter_map<O: ?Sized>(
myself: Self,
f: impl FnOnce(&mut T) -> Option<&mut O>,
) -> Option<WriteLock<'a, O, S, D>> {
let Self { write, data, .. } = myself;
let write = S::try_map_mut(write, f);
write.map(|write| WriteLock { write, data })
}
/// Downcast the lifetime of the mutable reference to the signal's value.
///
/// This function enforces the variance of the lifetime parameter `'a` in Mut. Rust will typically infer this cast with a concrete type, but it cannot with a generic type.
pub fn downcast_lifetime<'b>(mut_: Self) -> WriteLock<'b, T, S, D>
where
'a: 'b,
{
WriteLock {
write: S::downcast_lifetime_mut(mut_.write),
data: mut_.data,
}
}
}
impl<T, S, D> Deref for WriteLock<'_, T, S, D>
where
S: AnyStorage,
T: ?Sized,
{
type Target = T;
fn deref(&self) -> &Self::Target {
&self.write
}
}
impl<T, S, D> DerefMut for WriteLock<'_, T, S, D>
where
S: AnyStorage,
T: ?Sized,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.write
}
}
/// An extension trait for [`Writable`] that provides some convenience methods.
pub trait WritableExt: Writable {
/// Get a mutable reference to the value. If the value has been dropped, this will panic.
#[track_caller]
fn write(&mut self) -> WritableRef<'_, Self>
where
Self::Target: 'static,
{
self.try_write().unwrap()
}
/// Try to get a mutable reference to the value.
#[track_caller]
fn try_write(&mut self) -> Result<WritableRef<'_, Self>, generational_box::BorrowMutError>
where
Self::Target: 'static,
{
self.try_write_unchecked().map(WriteLock::downcast_lifetime)
}
/// Get a mutable reference to the value without checking the lifetime. This will update any subscribers.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
#[track_caller]
fn write_unchecked(&self) -> WritableRef<'static, Self>
where
Self::Target: 'static,
{
self.try_write_unchecked().unwrap()
}
/// Map the references and mutable references of the writable value to a new type. This lets you provide a view
/// into the writable value without creating a new signal or cloning the value.
///
/// Anything that subscribes to the writable value will be rerun whenever the original value changes or you write to this
/// scoped value, even if the view does not change. If you want to memorize the view, you can use a [`crate::Memo`] instead.
/// For fine grained scoped updates, use stores instead
///
/// # Example
/// ```rust
/// # use dioxus::prelude::*;
/// fn List(list: Signal<Vec<i32>>) -> Element {
/// rsx! {
/// for index in 0..list.len() {
/// // We can use the `map` method to provide a view into the single item in the list that the child component will render
/// Item { item: list.map_mut(move |v| &v[index], move |v| &mut v[index]) }
/// }
/// }
/// }
///
/// // The child component doesn't need to know that the mapped value is coming from a list
/// #[component]
/// fn Item(item: WriteSignal<i32>) -> Element {
/// rsx! {
/// button {
/// onclick: move |_| *item.write() += 1,
/// "{item}"
/// }
/// }
/// }
/// ```
fn map_mut<O, F, FMut>(self, f: F, f_mut: FMut) -> MappedMutSignal<O, Self, F, FMut>
where
Self: Sized,
O: ?Sized,
F: Fn(&Self::Target) -> &O,
FMut: Fn(&mut Self::Target) -> &mut O,
{
MappedMutSignal::new(self, f, f_mut)
}
/// Run a function with a mutable reference to the value. If the value has been dropped, this will panic.
#[track_caller]
fn with_mut<O>(&mut self, f: impl FnOnce(&mut Self::Target) -> O) -> O
where
Self::Target: 'static,
{
f(&mut *self.write())
}
/// Set the value of the signal. This will trigger an update on all subscribers.
#[track_caller]
fn set(&mut self, value: Self::Target)
where
Self::Target: Sized + 'static,
{
*self.write() = value;
}
/// Invert the boolean value of the signal. This will trigger an update on all subscribers.
#[track_caller]
fn toggle(&mut self)
where
Self::Target: std::ops::Not<Output = Self::Target> + Clone + 'static,
{
let inverted = !(*self.peek()).clone();
self.set(inverted);
}
/// Index into the inner value and return a reference to the result.
#[track_caller]
fn index_mut<I>(
&mut self,
index: I,
) -> WritableRef<'_, Self, <Self::Target as std::ops::Index<I>>::Output>
where
Self::Target: std::ops::IndexMut<I> + 'static,
{
WriteLock::map(self.write(), |v| v.index_mut(index))
}
/// Takes the value out of the Signal, leaving a Default in its place.
#[track_caller]
fn take(&mut self) -> Self::Target
where
Self::Target: Default + 'static,
{
self.with_mut(std::mem::take)
}
/// Replace the value in the Signal, returning the old value.
#[track_caller]
fn replace(&mut self, value: Self::Target) -> Self::Target
where
Self::Target: Sized + 'static,
{
self.with_mut(|v| std::mem::replace(v, value))
}
}
impl<W: Writable + ?Sized> WritableExt for W {}
/// An extension trait for [`Writable`] values that can be boxed into a trait object.
pub trait WritableBoxedExt: Writable<Storage = UnsyncStorage> {
/// Box the writable value into a trait object. This is useful for passing around writable values without knowing their concrete type.
fn boxed_mut(self) -> WriteSignal<Self::Target>
where
Self: Sized + 'static,
{
WriteSignal::new(self)
}
}
impl<T: Writable<Storage = UnsyncStorage> + 'static> WritableBoxedExt for T {
fn boxed_mut(self) -> WriteSignal<Self::Target> {
WriteSignal::new(self)
}
}
/// An extension trait for [`Writable<Option<T>>`]` that provides some convenience methods.
pub trait WritableOptionExt<T>: Writable<Target = Option<T>> {
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
#[track_caller]
fn get_or_insert(&mut self, default: T) -> WritableRef<'_, Self, T>
where
T: 'static,
{
self.get_or_insert_with(|| default)
}
/// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
#[track_caller]
fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> WritableRef<'_, Self, T>
where
T: 'static,
{
let is_none = self.read().is_none();
if is_none {
self.with_mut(|v| *v = Some(default()));
WriteLock::map(self.write(), |v| v.as_mut().unwrap())
} else {
WriteLock::map(self.write(), |v| v.as_mut().unwrap())
}
}
/// Attempts to write the inner value of the Option.
#[track_caller]
fn as_mut(&mut self) -> Option<WritableRef<'_, Self, T>>
where
T: 'static,
{
WriteLock::filter_map(self.write(), |v: &mut Option<T>| v.as_mut())
}
}
impl<T, W> WritableOptionExt<T> for W where W: Writable<Target = Option<T>> {}
/// An extension trait for [`Writable<Result<T, E>>`] that provides some convenience methods.
pub trait WritableResultExt<T, E>: Writable<Target = Result<T, E>> {
/// Unwraps the inner value mutably, panicking if the Result is an error.
#[track_caller]
fn unwrap_mut(&mut self) -> WritableRef<'_, Self, T>
where
T: 'static,
E: 'static,
{
WriteLock::filter_map(self.write(), |v| v.as_mut().ok())
.expect("Tried to unwrap a Result that was an error")
}
/// Attempts to mutably access the inner value of the Result.
#[track_caller]
fn as_mut(&mut self) -> Result<WritableRef<'_, Self, T>, WritableRef<'_, Self, E>>
where
T: 'static,
E: 'static,
{
let write = self.write();
match write.as_ref() {
Ok(_) => Ok(WriteLock::map(write, |v| {
v.as_mut()
.ok()
.expect("Result variant changed between read and write")
})),
Err(_) => Err(WriteLock::map(write, |v| {
v.as_mut()
.err()
.expect("Result variant changed between read and write")
})),
}
}
}
impl<T, E, W> WritableResultExt<T, E> for W where W: Writable<Target = Result<T, E>> {}
/// An extension trait for [`Writable<Vec<T>>`] that provides some convenience methods.
pub trait WritableVecExt<T>: Writable<Target = Vec<T>> {
/// Pushes a new value to the end of the vector.
#[track_caller]
fn push(&mut self, value: T)
where
T: 'static,
{
self.with_mut(|v| v.push(value))
}
/// Pops the last value from the vector.
#[track_caller]
fn pop(&mut self) -> Option<T>
where
T: 'static,
{
self.with_mut(|v| v.pop())
}
/// Inserts a new value at the given index.
#[track_caller]
fn insert(&mut self, index: usize, value: T)
where
T: 'static,
{
self.with_mut(|v| v.insert(index, value))
}
/// Removes the value at the given index.
#[track_caller]
fn remove(&mut self, index: usize) -> T
where
T: 'static,
{
self.with_mut(|v| v.remove(index))
}
/// Clears the vector, removing all values.
#[track_caller]
fn clear(&mut self)
where
T: 'static,
{
self.with_mut(|v| v.clear())
}
/// Extends the vector with the given iterator.
#[track_caller]
fn extend(&mut self, iter: impl IntoIterator<Item = T>)
where
T: 'static,
{
self.with_mut(|v| v.extend(iter))
}
/// Truncates the vector to the given length.
#[track_caller]
fn truncate(&mut self, len: usize)
where
T: 'static,
{
self.with_mut(|v| v.truncate(len))
}
/// Swaps two values in the vector.
#[track_caller]
fn swap_remove(&mut self, index: usize) -> T
where
T: 'static,
{
self.with_mut(|v| v.swap_remove(index))
}
/// Retains only the values that match the given predicate.
#[track_caller]
fn retain(&mut self, f: impl FnMut(&T) -> bool)
where
T: 'static,
{
self.with_mut(|v| v.retain(f))
}
/// Splits the vector into two at the given index.
#[track_caller]
fn split_off(&mut self, at: usize) -> Vec<T>
where
T: 'static,
{
self.with_mut(|v| v.split_off(at))
}
/// Try to mutably get an element from the vector.
#[track_caller]
fn get_mut(&mut self, index: usize) -> Option<WritableRef<'_, Self, T>>
where
T: 'static,
{
WriteLock::filter_map(self.write(), |v: &mut Vec<T>| v.get_mut(index))
}
/// Gets an iterator over the values of the vector.
#[track_caller]
fn iter_mut(&mut self) -> WritableValueIterator<'_, Self>
where
Self: Sized + Clone,
{
WritableValueIterator {
index: 0,
value: self,
}
}
}
/// An iterator over the values of a [`Writable<Vec<T>>`].
pub struct WritableValueIterator<'a, R> {
index: usize,
value: &'a mut R,
}
impl<'a, T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIterator<'a, R> {
type Item = WritableRef<'a, R, T>;
fn next(&mut self) -> Option<Self::Item> {
let index = self.index;
self.index += 1;
WriteLock::filter_map(
self.value.try_write_unchecked().unwrap(),
|v: &mut Vec<T>| v.get_mut(index),
)
.map(WriteLock::downcast_lifetime)
}
}
impl<W, T> WritableVecExt<T> for W where W: Writable<Target = Vec<T>> {}
/// An extension trait for [`Writable<String>`] that provides some convenience methods.
pub trait WritableStringExt: Writable<Target = String> {
ext_methods! {
/// Pushes a character to the end of the string.
fn push_str(&mut self, s: &str) = String::push_str;
/// Pushes a character to the end of the string.
fn push(&mut self, c: char) = String::push;
/// Pops a character from the end of the string.
fn pop(&mut self) -> Option<char> = String::pop;
/// Inserts a string at the given index.
fn insert_str(&mut self, idx: usize, s: &str) = String::insert_str;
/// Inserts a character at the given index.
fn insert(&mut self, idx: usize, c: char) = String::insert;
/// Remove a character at the given index
fn remove(&mut self, idx: usize) -> char = String::remove;
/// Replace a range of the string with the given string.
fn replace_range(&mut self, range: impl std::ops::RangeBounds<usize>, replace_with: &str) = String::replace_range;
/// Clears the string, removing all characters.
fn clear(&mut self) = String::clear;
/// Extends the string with the given iterator of characters.
fn extend(&mut self, iter: impl IntoIterator<Item = char>) = String::extend;
/// Truncates the string to the given length.
fn truncate(&mut self, len: usize) = String::truncate;
/// Splits the string off at the given index, returning the tail as a new string.
fn split_off(&mut self, at: usize) -> String = String::split_off;
}
}
impl<W> WritableStringExt for W where W: Writable<Target = String> {}
/// An extension trait for [`Writable<HashMap<K, V, H>>`] that provides some convenience methods.
pub trait WritableHashMapExt<K: 'static, V: 'static, H: 'static>:
Writable<Target = HashMap<K, V, H>>
{
ext_methods! {
/// Clears the map, removing all key-value pairs.
fn clear(&mut self) = HashMap::clear;
/// Retains only the key-value pairs that match the given predicate.
fn retain(&mut self, f: impl FnMut(&K, &mut V) -> bool) = HashMap::retain;
}
/// Inserts a key-value pair into the map. If the key was already present, the old value is returned.
#[track_caller]
fn insert(&mut self, k: K, v: V) -> Option<V>
where
K: std::cmp::Eq + std::hash::Hash,
H: std::hash::BuildHasher,
{
self.with_mut(|map: &mut HashMap<K, V, H>| map.insert(k, v))
}
/// Extends the map with the key-value pairs from the given iterator.
#[track_caller]
fn extend(&mut self, iter: impl IntoIterator<Item = (K, V)>)
where
K: std::cmp::Eq + std::hash::Hash,
H: std::hash::BuildHasher,
{
self.with_mut(|map: &mut HashMap<K, V, H>| map.extend(iter))
}
/// Removes a key from the map, returning the value at the key if the key was previously in the map.
#[track_caller]
fn remove(&mut self, k: &K) -> Option<V>
where
K: std::cmp::Eq + std::hash::Hash,
H: std::hash::BuildHasher,
{
self.with_mut(|map: &mut HashMap<K, V, H>| map.remove(k))
}
/// Get a mutable reference to the value at the given key.
#[track_caller]
fn get_mut(&mut self, k: &K) -> Option<WritableRef<'_, Self, V>>
where
K: std::cmp::Eq + std::hash::Hash,
H: std::hash::BuildHasher,
{
WriteLock::filter_map(self.write(), |map: &mut HashMap<K, V, H>| map.get_mut(k))
}
}
impl<K: 'static, V: 'static, H: 'static, R> WritableHashMapExt<K, V, H> for R where
R: Writable<Target = HashMap<K, V, H>>
{
}
/// An extension trait for [`Writable<HashSet<V, H>>`] that provides some convenience methods.
pub trait WritableHashSetExt<V: 'static, H: 'static>: Writable<Target = HashSet<V, H>> {
ext_methods! {
/// Clear the hash set.
fn clear(&mut self) = HashSet::clear;
/// Retain only the elements specified by the predicate.
fn retain(&mut self, f: impl FnMut(&V) -> bool) = HashSet::retain;
}
/// Inserts a value into the set. Returns true if the value was not already present.
#[track_caller]
fn insert(&mut self, k: V) -> bool
where
V: std::cmp::Eq + std::hash::Hash,
H: std::hash::BuildHasher,
{
self.with_mut(|set| set.insert(k))
}
/// Extends the set with the values from the given iterator.
#[track_caller]
fn extend(&mut self, iter: impl IntoIterator<Item = V>)
where
V: std::cmp::Eq + std::hash::Hash,
H: std::hash::BuildHasher,
{
self.with_mut(|set| set.extend(iter))
}
/// Removes a value from the set. Returns true if the value was present.
#[track_caller]
fn remove(&mut self, k: &V) -> bool
where
V: std::cmp::Eq + std::hash::Hash,
H: std::hash::BuildHasher,
{
self.with_mut(|set| set.remove(k))
}
}
impl<V: 'static, H: 'static, R> WritableHashSetExt<V, H> for R where
R: Writable<Target = HashSet<V, H>>
{
}