Skip to main content

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.peek_unchecked())),
36            Box::new(|_| true),
37        )
38    }
39}
40
41impl<T: 'static, Value: 'static, Channel: RadioChannel<Value> + 'static> IntoReadable<T>
42    for RadioSlice<Value, T, Channel>
43{
44    fn into_readable(self) -> Readable<T> {
45        Readable::from_slice(self)
46    }
47}
48
49impl<T: 'static, Value: 'static, Channel: RadioChannel<Value> + 'static> IntoReadable<T>
50    for RadioSliceMut<Value, T, Channel>
51{
52    fn into_readable(self) -> Readable<T> {
53        Readable::new(
54            Box::new({
55                let this = self.clone();
56                move || ReadableRef::Ref(this.read_unchecked())
57            }),
58            Box::new(move || ReadableRef::Ref(self.peek_unchecked())),
59            Box::new(|_| true),
60        )
61    }
62}