-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathipmpsc-receive.rs
More file actions
75 lines (64 loc) · 2.15 KB
/
ipmpsc-receive.rs
File metadata and controls
75 lines (64 loc) · 2.15 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![deny(warnings)]
use clap::{App, Arg};
use ipmpsc::{Receiver, SharedRingBuffer, ShmDeserializer, ShmZeroCopyDeserializer};
use serde::Deserialize;
#[derive(Debug)]
pub struct BincodeZeroCopyDeserializer<T>(pub T);
impl<'de, T> ShmZeroCopyDeserializer<'de> for BincodeZeroCopyDeserializer<T>
where
T: Deserialize<'de>,
{
type Error = bincode::Error;
fn deserialize_from_bytes(bytes: &'de [u8]) -> std::result::Result<Self, Self::Error> {
Ok(Self(bincode::deserialize::<T>(bytes)?))
}
}
#[derive(Debug)]
pub struct BincodeDeserializer<T>(pub T);
impl<T> ShmDeserializer for BincodeDeserializer<T>
where
T: for<'de> Deserialize<'de>,
{
type Error = bincode::Error;
fn deserialize_from_bytes<'de>(bytes: &'de [u8]) -> std::result::Result<Self, Self::Error> {
Ok(Self(bincode::deserialize::<T>(bytes)?))
}
}
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 file will be cleared if it already exists or created if it doesn't.",
)
.required(true),
)
.arg(
Arg::with_name("zero copy")
.long("zero-copy")
.help("Use zero-copy deserialization"),
)
.get_matches();
let map_file = matches.value_of("map file").unwrap();
let mut rx = Receiver::new(SharedRingBuffer::create(map_file, 32 * 1024)?);
let zero_copy = matches.is_present("zero copy");
println!(
"Ready! Now run `cargo run --example ipmpsc-send {}` in another terminal.",
map_file
);
loop {
if zero_copy {
println!(
"received {:?}",
rx.zero_copy_context()
.recv::<BincodeZeroCopyDeserializer<&str>>()?
);
} else {
println!("received {:?}", rx.recv::<BincodeDeserializer<String>>()?);
}
}
}