freya_radio/
readable.rs

1use freya_core::lifecycle::{
2    readable::{
3        IntoReadable,
4        Readable,
5    },
6    state::ReadableRef,
7};
8
9use crate::{
10    hooks::RadioChannel,
11    slice::{
12        RadioSlice,
13        RadioSliceMut,
14    },
15};
16
17pub trait RadioReadable<T: 'static> {
18    fn from_slice<Value, Channel>(slice: RadioSlice<Value, T, Channel>) -> Self
19    where
20        Value: 'static,
21        Channel: RadioChannel<Value> + 'static;
22}
23
24impl<T: 'static> RadioReadable<T> for Readable<T> {
25    fn from_slice<Value, Channel>(slice: RadioSlice<Value, T, Channel>) -> Self
26    where
27        Value: 'static,
28        Channel: RadioChannel<Value> + 'static,
29    {
30        Self::new(
31            Box::new({
32                let slice = slice.clone();
33                move || ReadableRef::Ref(slice.read_unchecked())
34            }),
35            Box::new(move || ReadableRef::Ref(slice.read_unchecked())),
36        )
37    }
38}
39
40impl<T: 'static, Value: 'static, Channel: RadioChannel<Value> + 'static> IntoReadable<T>
41    for RadioSlice<Value, T, Channel>
42{
43    fn into_readable(self) -> Readable<T> {
44        Readable::from_slice(self)
45    }
46}
47
48impl<T: 'static, Value: 'static, Channel: RadioChannel<Value> + 'static> IntoReadable<T>
49    for RadioSliceMut<Value, T, Channel>
50{
51    fn into_readable(self) -> Readable<T> {
52        Readable::new(
53            Box::new({
54                let this = self.clone();
55                move || ReadableRef::Ref(this.read_unchecked())
56            }),
57            Box::new(move || ReadableRef::Ref(self.peek_unchecked())),
58        )
59    }
60}