Trait transformable_channels::mpsc::TransformableSender [] [src]

pub trait TransformableSender<V>: Send + 'static where V: Send + 'static, Self: ExtSender<V> + Clone {
    fn map<F, T>(&self, f: F) -> MappedSender<F, T, V, Self> where Self: Sized, F: Fn(T) -> V + Sync + Send + 'static, T: Send + 'static, V: Send + 'static { ... }
    fn filter<F>(&self, f: F) -> FilteredSender<F, V, Self> where Self: Sized, F: Fn(&V) -> bool + Sync + Send + 'static { ... }
    fn filter_map<F, T>(&self, f: F) -> FilterMappedSender<F, T, V, Self> where Self: Sized, F: Fn(T) -> Option<V> + Sync + Send + 'static, T: Send + 'static { ... }
    fn tie<S, W>(&self, other: &S) -> TiedSender<Self, S, V, W> where Self: Sized, S: ExtSender<W> + TransformableSender<W>, W: Send + 'static { ... }
}

Provided Methods

fn map<F, T>(&self, f: F) -> MappedSender<F, T, V, Self> where Self: Sized, F: Fn(T) -> V + Sync + Send + 'static, T: Send + 'static, V: Send + 'static

From an ExtSender, derive a new ExtSender with the same Receiver, but which transforms values prior to transmitting them.

This allows, for instance, converting from one data type to another one, or labelling values with their origin.

Note that the closure will often be shared between threads and must therefore implement Sync.

Safety and performance

Argument f is executed during the call to send. Therefore, any panic in f will cause the sender thread to panic. If f is slow, the sender thread will be slowed down.

Example

use transformable_channels::mpsc::*;

let (tx, rx) = channel();

#[derive(Debug)]
struct LabelledValue<T> {
  origin: usize,
  value: T,
}

for i in 1 .. 5 {
  // Let us label messages as they are sent.
  let tx = tx.map(move |msg| {
     LabelledValue {
       origin: i,
       value: msg,
     }
  });

  tx.send(i).unwrap();
}

for _ in 1 .. 5 {
  let msg = rx.recv().unwrap();
  assert_eq!(msg.origin, msg.value);
}

fn filter<F>(&self, f: F) -> FilteredSender<F, V, Self> where Self: Sized, F: Fn(&V) -> bool + Sync + Send + 'static

From an ExtSender, derive a new ExtSender with the same Receiver, but which may decide to discard some values instead of transmitting them.

This allows, for instance, discarding events that are not of interest.

Note that the closure will often be shared between threads and must therefore implement Sync.

Safety and performance

Argument f is executed during the call to send. Therefore, any panic in f will cause the sender thread to panic. If f is slow, the sender thread will be slowed down.

Example

use transformable_channels::mpsc::*;

let (tx, rx) = channel();

// We are only interested in even messages
let tx = tx.filter(move |msg| {
   msg % 2 == 0
});

for i in 1 .. 10 {
  tx.send(i).unwrap();
}

for _ in 1 .. 5 {
  assert!(rx.recv().unwrap() % 2 == 0);
}

fn filter_map<F, T>(&self, f: F) -> FilterMappedSender<F, T, V, Self> where Self: Sized, F: Fn(T) -> Option<V> + Sync + Send + 'static, T: Send + 'static

From an ExtSender, derive a new ExtSender with the same Receiver, but which may both transform values priori to transmitting them, or to discard them entirely.

This combines the features of map and filter.

Note that the closure will often be shared between threads and must therefore implement Sync.

Safety and performance

Argument f is executed during the call to send. Therefore, any panic in f will cause the sender thread to panic. If f is slow, the sender thread will be slowed down.

Example

use transformable_channels::mpsc::*;

let (tx, rx) = channel();

// We are only interested in even messages
let tx = tx.filter_map(move |msg| {
   if msg % 2 == 0 {
     Some( msg + 1 )
   } else {
     None
   }
});

for i in 1 .. 10 {
  tx.send(i).unwrap();
}

for i in 1 .. 5 {
  assert_eq!(rx.recv().unwrap(), i * 2 + 1);
}

fn tie<S, W>(&self, other: &S) -> TiedSender<Self, S, V, W> where Self: Sized, S: ExtSender<W> + TransformableSender<W>, W: Send + 'static

From two ExtSenders, derive a new ExtSender which sends to both ExtSenders.

Example

use transformable_channels::mpsc::*;

let (tx_1, rx_1) = channel();
let (tx_2, rx_2) = channel();

let tx = tx_1.tie(&tx_2);

tx.send(("one".to_string(), 1)).unwrap();
assert_eq!(rx_1.recv().unwrap(), "one".to_string());
assert_eq!(rx_2.recv().unwrap(), 1);

Implementors