-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathipmpsc-send.rs
More file actions
49 lines (39 loc) · 1.42 KB
/
ipmpsc-send.rs
File metadata and controls
49 lines (39 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![deny(warnings)]
use clap::{App, Arg};
use ipmpsc::{Sender, SharedRingBuffer, ShmSerializer};
use serde::Serialize;
use std::io::{self, BufRead};
#[derive(Debug)]
pub struct BincodeSerializer<T: Serialize>(pub T);
impl<T: Serialize> ShmSerializer for BincodeSerializer<T> {
type Error = bincode::Error;
fn serialize(&self) -> std::result::Result<Vec<u8>, Self::Error> {
Ok(bincode::serialize(&self.0)?)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let matches = App::new("ipmpsc-send")
.about("ipmpsc sender example")
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.arg(
Arg::with_name("map file")
.help(
"File to use for shared memory ring buffer. \
This should have already been created and initialized by the receiver.",
)
.required(true),
)
.get_matches();
let map_file = matches.value_of("map file").unwrap();
let tx = Sender::new(SharedRingBuffer::open(map_file)?);
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
println!("Ready! Enter some lines of text to send them to the receiver.");
while handle.read_line(&mut buffer)? > 0 {
tx.send::<BincodeSerializer<&String>>(&BincodeSerializer(&buffer))?;
buffer.clear();
}
Ok(())
}