new_lambda_sink_with_input_from

Function new_lambda_sink_with_input_from 

Source
pub fn new_lambda_sink_with_input_from<T: Send + 'static, V: Clone + Send + 'static>(
    input_receiver: Receiver<T>,
    shared_resource: V,
    function: fn(&V, T),
) -> ThreadPool
Expand description

Connects an existing channel to a new ThreadPool to create a multi-producer multi-threaded lambda-sink.

ยงExamples

use std::collections::HashMap;
use rand::{seq::SliceRandom, thread_rng};

use lambda_channel::new_lambda_sink_with_input_from;
use lambda_channel::channel::new_channel;

fn generate_cipher_map() -> HashMap<char, char> {
    let mut rng = thread_rng();
    let mut alphanumeric: Vec<char> = Vec::new();
    alphanumeric.extend('0'..='9');
    alphanumeric.extend('a'..='z');
    alphanumeric.extend('A'..='Z');
     
    let mut shuffled_alphanumeric = alphanumeric.clone();
    shuffled_alphanumeric.shuffle(&mut rng);

    let mut cipher_map = HashMap::new();
    for (i, &char_val) in alphanumeric.iter().enumerate() {
        cipher_map.insert(char_val, shuffled_alphanumeric[i]);
    }
    cipher_map
}

fn encode_text(cipher: &HashMap<char, char>, text: String) {
    let mut encoded_text = String::new();
    println!("Encoding:    {}", text);
    for c in text.chars() {
        if let Some(v) = cipher.get(&c) {
            encoded_text.push(*v)
        } else {
            encoded_text.push(c)
        }
    }
    println!("Cipher Text: {}", encoded_text);
}

let cipher_map = generate_cipher_map();
let (s, r) = new_channel(Some(0));
let _p = new_lambda_sink_with_input_from(r, cipher_map, encode_text);
s.send("This is a test!".to_string()).unwrap();