use std::convert::Infallible;
use crate::{context::Context, observable::ObservableType, observer::Observer};
#[derive(Clone)]
pub struct Of<T>(pub T);
impl<T> ObservableType for Of<T> {
type Item<'a>
= T
where
T: 'a;
type Err = Infallible;
}
impl<C, T> crate::observable::CoreObservable<C> for Of<T>
where
C: Context,
C::Inner: Observer<T, Infallible>,
{
type Unsub = ();
fn subscribe(self, context: C) -> Self::Unsub {
let mut observer = context.into_inner();
observer.next(self.0);
observer.complete();
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[rxrust_macro::test(local)]
async fn test_of_emits_value() {
use std::{cell::RefCell, rc::Rc};
let result = Rc::new(RefCell::new(None));
let result_clone = result.clone();
Local::of(42).subscribe(move |v| {
*result_clone.borrow_mut() = Some(v);
});
assert_eq!(*result.borrow(), Some(42));
}
#[rxrust_macro::test(local)]
async fn test_of_with_different_types() {
use std::{cell::RefCell, rc::Rc};
let string_result = Rc::new(RefCell::new(None));
let string_result_clone = string_result.clone();
Local::of("hello".to_string()).subscribe(move |v| {
*string_result_clone.borrow_mut() = Some(v);
});
assert_eq!(*string_result.borrow(), Some("hello".to_string()));
let bool_result = Rc::new(RefCell::new(None));
let bool_result_clone = bool_result.clone();
Local::of(true).subscribe(move |v| {
*bool_result_clone.borrow_mut() = Some(v);
});
assert_eq!(*bool_result.borrow(), Some(true));
}
}