freya_radio/
writable.rs

1use freya_core::lifecycle::writable::{
2    IntoWritable,
3    Writable,
4};
5
6use crate::{
7    hooks::RadioChannel,
8    slice::RadioSliceMut,
9};
10
11pub trait RadioWritable<T: 'static> {
12    fn from_slice<Value, Channel>(slice: RadioSliceMut<Value, T, Channel>) -> Self
13    where
14        Value: 'static,
15        Channel: RadioChannel<Value> + 'static;
16}
17
18impl<T: 'static> RadioWritable<T> for Writable<T> {
19    fn from_slice<Value, Channel>(slice: RadioSliceMut<Value, T, Channel>) -> Self
20    where
21        Value: 'static,
22        Channel: RadioChannel<Value> + 'static,
23    {
24        Self::new(
25            Box::new({
26                let slice = slice.clone();
27                move || slice.peek_unchecked()
28            }),
29            Box::new({
30                let slice = slice.clone();
31                move || slice.write_unchecked_no_notify()
32            }),
33            Box::new({
34                let slice = slice.clone();
35                move || slice.subscribe_if_not()
36            }),
37            Box::new(move || slice.notify()),
38        )
39    }
40}
41
42impl<T: 'static, Value: 'static, Channel: RadioChannel<Value> + 'static> IntoWritable<T>
43    for RadioSliceMut<Value, T, Channel>
44{
45    fn into_writable(self) -> Writable<T> {
46        Writable::from_slice(self)
47    }
48}