wireshift-core 0.1.1

Core typed operations, buffers, and backend traits for wireshift
Documentation
//! Operations representing cancellation requests over previous instances.
use crate::error::Result;
use crate::op::{CompletionPayload, Op, OpDescriptor};

/// Internal operation used by the backend for request cancellation.
///
/// ```rust
/// use wireshift::ops::Cancel;
///
/// let cancel = Cancel::new(9);
/// assert_eq!(cancel.target(), 9);
/// ```
#[derive(Clone, Copy, Debug)]
pub struct Cancel {
    target: u64,
}

impl Cancel {
    /// Creates a cancel request targeting an existing request id.
    #[must_use]
    pub fn new(target: u64) -> Self {
        Self { target }
    }

    /// Returns the targeted request id.
    #[must_use]
    pub fn target(self) -> u64 {
        self.target
    }
}

impl Op for Cancel {
    type Output = ();

    fn name(&self) -> &'static str {
        "cancel"
    }

    fn into_descriptor(self) -> Result<OpDescriptor> {
        Ok(OpDescriptor::Cancel {
            target: self.target,
        })
    }

    fn map_completion(payload: CompletionPayload) -> Result<Self::Output> {
        match payload {
            CompletionPayload::Unit => Ok(()),
            other => Err(crate::error::Error::completion(
                format!("cancel received unexpected completion payload: {other:?}"),
                "ensure the backend routes cancel completions as unit payloads",
            )),
        }
    }
}