Skip to main content

Options

Struct Options 

Source
#[non_exhaustive]
pub struct Options { pub name: String, pub path: PathBuf, pub capacity: usize, }
Expand description

Queue identity and message-buffer capacity. Every participant must agree.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§name: String

Queue name; use at most 24 UTF-8 bytes for portability.

§path: PathBuf

Shared storage directory on Unix; ignored on Windows.

§capacity: usize

Message buffer bytes, excluding metadata; greater than 16 and divisible by 8.

Implementations§

Source§

impl Options

Source

pub fn new(name: impl Into<String>, capacity: usize) -> Self

Uses the operating system temporary directory for shared storage.

Examples found in repository?
examples/benchmark.rs (line 8)
5fn main() {
6    const ITERATIONS: usize = 1_000_000;
7    for size in [3, 50, 1024] {
8        let options = Options::new(format!("b{}x{size}", std::process::id()), 1 << 20);
9        let publisher = Publisher::open(&options).unwrap();
10        let subscriber = Subscriber::open(&options).unwrap();
11        let message = vec![42; size];
12        let mut received = vec![0; size];
13        let mut samples = Vec::new();
14        for round in 0..12 {
15            let start = Instant::now();
16            for _ in 0..ITERATIONS {
17                publisher.try_send(black_box(&message)).unwrap();
18                assert_eq!(
19                    subscriber.try_recv_into(black_box(&mut received)).unwrap(),
20                    Some(size)
21                );
22                black_box(&received);
23            }
24            if round >= 4 {
25                samples.push(start.elapsed().as_nanos() as f64 / ITERATIONS as f64);
26            }
27        }
28        let mean = samples.iter().sum::<f64>() / samples.len() as f64;
29        let deviation = (samples.iter().map(|x| (x - mean).powi(2)).sum::<f64>()
30            / (samples.len() - 1) as f64)
31            .sqrt();
32        println!(
33            "{size} bytes: {mean:.2} ns/roundtrip, stddev {deviation:.2}, samples {samples:?}"
34        );
35    }
36}
More examples
Hide additional examples
examples/interop.rs (lines 16-21)
14fn main() {
15    let args = std::env::args().collect::<Vec<_>>();
16    let options = Options::new(
17        &args[2],
18        std::env::var("INTEROP_CAPACITY")
19            .map(|v| v.parse().unwrap())
20            .unwrap_or(4096),
21    )
22    .with_path(&args[3]);
23    let count = args[4].parse::<usize>().unwrap();
24    if args[1].starts_with("hold-") {
25        let _publisher;
26        let _subscriber;
27        if args[1] == "hold-publisher" {
28            _publisher = Publisher::open(&options).unwrap();
29        } else {
30            _subscriber = Subscriber::open(&options).unwrap();
31        }
32        println!("READY");
33        io::stdout().flush().unwrap();
34        io::stdin().read_line(&mut String::new()).unwrap();
35    } else if args[1] == "publish" {
36        let publisher = Publisher::open(&options).unwrap();
37        let start = args.get(5).map_or(0, |s| s.parse::<usize>().unwrap());
38        if args.len() > 5 {
39            println!("READY");
40            io::stdout().flush().unwrap();
41            io::stdin().read_line(&mut String::new()).unwrap();
42        }
43        for i in start..start + count {
44            let data = message(i);
45            while let Err(error) = publisher.try_send(&data) {
46                assert!(error.is_full(), "{error}");
47                std::thread::yield_now();
48            }
49        }
50    } else {
51        let subscriber = Subscriber::open(&options).unwrap();
52        println!("READY");
53        if args[1] == "collect" {
54            loop {
55                let data = subscriber
56                    .recv_timeout(Duration::from_secs(30))
57                    .unwrap()
58                    .unwrap();
59                if data.is_empty() {
60                    break;
61                }
62                let id = u64::from_le_bytes(data[..8].try_into().unwrap()) as usize;
63                assert_eq!(data, message(id));
64                println!("{id}");
65            }
66            return;
67        }
68        for i in 0..count {
69            assert_eq!(
70                subscriber
71                    .recv_timeout(Duration::from_secs(30))
72                    .unwrap()
73                    .unwrap(),
74                message(i)
75            );
76        }
77    }
78}
Source

pub fn with_path(self, path: impl Into<PathBuf>) -> Self

Selects a shared storage directory on Unix.

Examples found in repository?
examples/interop.rs (line 22)
14fn main() {
15    let args = std::env::args().collect::<Vec<_>>();
16    let options = Options::new(
17        &args[2],
18        std::env::var("INTEROP_CAPACITY")
19            .map(|v| v.parse().unwrap())
20            .unwrap_or(4096),
21    )
22    .with_path(&args[3]);
23    let count = args[4].parse::<usize>().unwrap();
24    if args[1].starts_with("hold-") {
25        let _publisher;
26        let _subscriber;
27        if args[1] == "hold-publisher" {
28            _publisher = Publisher::open(&options).unwrap();
29        } else {
30            _subscriber = Subscriber::open(&options).unwrap();
31        }
32        println!("READY");
33        io::stdout().flush().unwrap();
34        io::stdin().read_line(&mut String::new()).unwrap();
35    } else if args[1] == "publish" {
36        let publisher = Publisher::open(&options).unwrap();
37        let start = args.get(5).map_or(0, |s| s.parse::<usize>().unwrap());
38        if args.len() > 5 {
39            println!("READY");
40            io::stdout().flush().unwrap();
41            io::stdin().read_line(&mut String::new()).unwrap();
42        }
43        for i in start..start + count {
44            let data = message(i);
45            while let Err(error) = publisher.try_send(&data) {
46                assert!(error.is_full(), "{error}");
47                std::thread::yield_now();
48            }
49        }
50    } else {
51        let subscriber = Subscriber::open(&options).unwrap();
52        println!("READY");
53        if args[1] == "collect" {
54            loop {
55                let data = subscriber
56                    .recv_timeout(Duration::from_secs(30))
57                    .unwrap()
58                    .unwrap();
59                if data.is_empty() {
60                    break;
61                }
62                let id = u64::from_le_bytes(data[..8].try_into().unwrap()) as usize;
63                assert_eq!(data, message(id));
64                println!("{id}");
65            }
66            return;
67        }
68        for i in 0..count {
69            assert_eq!(
70                subscriber
71                    .recv_timeout(Duration::from_secs(30))
72                    .unwrap()
73                    .unwrap(),
74                message(i)
75            );
76        }
77    }
78}

Trait Implementations§

Source§

impl Clone for Options

Source§

fn clone(&self) -> Options

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Options

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.