simplify singleton channel
we can directly use Option<T> without wrapping it
This commit is contained in:
parent
166e75a5c7
commit
9d9f21405b
1 changed files with 5 additions and 22 deletions
|
@ -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>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue