ibverbs-rs 0.4.1

Safe, ergonomic Rust bindings for the InfiniBand libibverbs API
Documentation
use crate::ibverbs::work::error::WorkError;
use crate::ibverbs::work::success::WorkSuccess;
use ibverbs_sys::ibv_wc;

/// A convenience alias for the result of a work request.
pub type WorkResult = Result<WorkSuccess, WorkError>;

/// Represents the completion of an asynchronous Work Request.
///
/// A `WorkCompletion` is generated by the RDMA hardware and placed into a Completion Queue (CQ).
/// It corresponds to a previously posted Work Request (Send, Receive, RDMA Write, etc.).
///
/// # Structure
///
/// * **ID** — The `wr_id` allows you to map this completion back to the specific request posted.
/// * **Result** — Indicates whether the operation succeeded or failed.
#[derive(Copy, Clone, Debug)]
pub struct WorkCompletion {
    wr_id: u64,
    result: WorkResult,
}

impl WorkCompletion {
    pub(crate) fn new(wc: ibv_wc) -> Self {
        Self {
            wr_id: wc.wr_id(),
            result: if let Some((error_code, vendor_code)) = wc.error() {
                Err(WorkError::new(error_code, vendor_code))
            } else {
                Ok(WorkSuccess::new(wc.imm_data(), wc.len()))
            },
        }
    }
}

impl WorkCompletion {
    /// Returns the 64-bit identifier assigned to the original Work Request.
    /// This value is passed through transparently by the hardware.
    pub fn wr_id(&self) -> u64 {
        self.wr_id
    }

    /// Returns the outcome of the operation.
    ///
    /// * `Ok(WorkSuccess)` — The operation completed successfully. Contains bytes transferred and immediate data.
    /// * `Err(WorkError)` — The operation failed. Contains the error status and vendor syndrome.
    pub fn result(&self) -> Result<WorkSuccess, WorkError> {
        self.result
    }
}