Created
January 10, 2020 14:44
-
-
Save njam/c6eff91f490d04c9f215248973cd3e1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::pin::Pin; | |
use std::rc::Rc; | |
use std::task::Poll; | |
use futures_executor::block_on; | |
use futures_util::future::poll_fn; | |
use futures_signals::signal::{Mutable, Signal}; | |
use futures_signals::signal_vec::{MutableVec}; | |
use futures_signals::signal_vec::SignalVecExt; | |
#[test] | |
fn to_signal_cloned() { | |
let mutable_vec = MutableVec::new_with_values(vec![3, 1, 6, 2]); | |
let list = mutable_vec.signal_vec().map(Rc::new); | |
#[derive(Copy, Clone)] | |
enum SortOption { Incr, Decr } | |
let sort_option = Mutable::new(SortOption::Incr); | |
let mut signal = futures_signals::map_mut! { | |
let list = list.to_signal_cloned(), | |
let sort_option = sort_option.signal() => { | |
list.sort_by(|left, right| { | |
match *sort_option { | |
SortOption::Incr => left.cmp(&right), | |
SortOption::Decr => right.cmp(&left), | |
} | |
}); | |
list.to_vec() | |
} | |
}; | |
let mut diffs = move || { | |
block_on(poll_fn(|context| { | |
let mut output = Vec::new(); | |
loop { | |
let poll = Pin::new(&mut signal).poll_change(context); | |
return match poll { | |
Poll::Ready(Some(item)) => { | |
output.push(item); | |
continue; | |
} | |
_ => { | |
Poll::Ready(output) | |
} | |
}; | |
} | |
})) | |
}; | |
assert_eq!(diffs(), vec!( | |
vec![Rc::new(1), Rc::new(2), Rc::new(3), Rc::new(6)], | |
)); | |
sort_option.set(SortOption::Decr); | |
assert_eq!(diffs(), vec!( | |
vec![Rc::new(6), Rc::new(3), Rc::new(2), Rc::new(1)], | |
)); | |
mutable_vec.lock_mut().replace(vec!(1, 2, 3)); | |
assert_eq!(diffs(), vec!( | |
vec![Rc::new(3), Rc::new(2), Rc::new(1)], | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment