safer_ring/advanced/
multi_shot.rs

1//! Multi-shot operation configuration.
2
3/// Multi-shot operation configuration.
4///
5/// Multi-shot operations allow a single submission to generate multiple
6/// completion events, reducing submission overhead for operations like
7/// `accept()` that can be repeated.
8#[derive(Debug, Clone, Default)]
9pub struct MultiShotConfig {
10    /// Maximum number of completions to generate
11    pub max_completions: Option<u32>,
12    /// Whether to continue on errors
13    pub continue_on_error: bool,
14    /// Buffer group to use for multi-shot reads
15    pub buffer_group_id: Option<u16>,
16}
17
18impl MultiShotConfig {
19    /// Create a new multi-shot configuration.
20    ///
21    /// # Examples
22    ///
23    /// ```rust
24    /// use safer_ring::advanced::MultiShotConfig;
25    ///
26    /// let config = MultiShotConfig::new()
27    ///     .with_max_completions(100)
28    ///     .with_buffer_group(1);
29    /// ```
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Set the maximum number of completions.
35    pub fn with_max_completions(mut self, max: u32) -> Self {
36        self.max_completions = Some(max);
37        self
38    }
39
40    /// Enable continuing on errors.
41    pub fn with_continue_on_error(mut self) -> Self {
42        self.continue_on_error = true;
43        self
44    }
45
46    /// Set the buffer group ID for multi-shot reads.
47    pub fn with_buffer_group(mut self, group_id: u16) -> Self {
48        self.buffer_group_id = Some(group_id);
49        self
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_default_config() {
59        let config = MultiShotConfig::default();
60
61        assert!(config.max_completions.is_none());
62        assert!(!config.continue_on_error);
63        assert!(config.buffer_group_id.is_none());
64    }
65
66    #[test]
67    fn test_builder_pattern() {
68        let config = MultiShotConfig::new()
69            .with_max_completions(50)
70            .with_continue_on_error()
71            .with_buffer_group(2);
72
73        assert_eq!(config.max_completions, Some(50));
74        assert!(config.continue_on_error);
75        assert_eq!(config.buffer_group_id, Some(2));
76    }
77}