Skip to main content

wireshift_core/ops/
cancel.rs

1//! Operations representing cancellation requests over previous instances.
2use crate::error::Result;
3use crate::op::{CompletionPayload, Op, OpDescriptor};
4
5/// Internal operation used by the backend for request cancellation.
6///
7/// ```rust
8/// use wireshift::ops::Cancel;
9///
10/// let cancel = Cancel::new(9);
11/// assert_eq!(cancel.target(), 9);
12/// ```
13#[derive(Clone, Copy, Debug)]
14pub struct Cancel {
15    target: u64,
16}
17
18impl Cancel {
19    /// Creates a cancel request targeting an existing request id.
20    #[must_use]
21    pub fn new(target: u64) -> Self {
22        Self { target }
23    }
24
25    /// Returns the targeted request id.
26    #[must_use]
27    pub fn target(self) -> u64 {
28        self.target
29    }
30}
31
32impl Op for Cancel {
33    type Output = ();
34
35    fn name(&self) -> &'static str {
36        "cancel"
37    }
38
39    fn into_descriptor(self) -> Result<OpDescriptor> {
40        Ok(OpDescriptor::Cancel {
41            target: self.target,
42        })
43    }
44
45    fn map_completion(payload: CompletionPayload) -> Result<Self::Output> {
46        match payload {
47            CompletionPayload::Unit => Ok(()),
48            other => Err(crate::error::Error::completion(
49                format!("cancel received unexpected completion payload: {other:?}"),
50                "ensure the backend routes cancel completions as unit payloads",
51            )),
52        }
53    }
54}