Skip to main content

freya_radio/
writable.rs

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