simplify singleton channel

we can directly use Option<T> without wrapping it
This commit is contained in:
Michael Sippel 2020-12-05 11:31:03 +01:00
parent 166e75a5c7
commit 9d9f21405b
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -55,26 +55,9 @@ impl<T: Eq + Hash> ChannelData for HashSet<T> {
Singleton Channel Singleton Channel
<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> <<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
\*/ \*/
pub struct SingletonChannel<T>(Option<T>); impl<T> ChannelData for Option<T> {
impl<T> Default for SingletonChannel<T> {
fn default() -> Self {
SingletonChannel(None)
}
}
impl<T> IntoIterator for SingletonChannel<T> {
type Item = T;
type IntoIter = std::iter::Once<T>;
fn into_iter(mut self) -> Self::IntoIter {
std::iter::once(self.0.take().unwrap())
}
}
impl<T> ChannelData for SingletonChannel<T> {
fn channel_insert(&mut self, x: T) { fn channel_insert(&mut self, x: T) {
self.0 = Some(x); *self = Some(x);
} }
} }
@ -184,11 +167,11 @@ pub fn set_channel<T: Eq + Hash>() -> (ChannelSender<HashSet<T>>, ChannelReceive
channel::<HashSet<T>>() channel::<HashSet<T>>()
} }
pub fn queue_channel<T: Eq + Hash>() -> (ChannelSender<Vec<T>>, ChannelReceiver<Vec<T>>) { pub fn queue_channel<T>() -> (ChannelSender<Vec<T>>, ChannelReceiver<Vec<T>>) {
channel::<Vec<T>>() channel::<Vec<T>>()
} }
pub fn singleton_channel<T: Eq + Hash>() -> (ChannelSender<SingletonChannel<T>>, ChannelReceiver<SingletonChannel<T>>) { pub fn singleton_channel<T>() -> (ChannelSender<Option<T>>, ChannelReceiver<Option<T>>) {
channel::<SingletonChannel<T>>() channel::<Option<T>>()
} }