Skip to main content

ibverbs_rs/ibverbs/work/
completion.rs

1use crate::ibverbs::work::error::WorkError;
2use crate::ibverbs::work::success::WorkSuccess;
3use ibverbs_sys::ibv_wc;
4
5/// A convenience alias for the result of a work request.
6pub type WorkResult = Result<WorkSuccess, WorkError>;
7
8/// Represents the completion of an asynchronous Work Request.
9///
10/// A `WorkCompletion` is generated by the RDMA hardware and placed into a Completion Queue (CQ).
11/// It corresponds to a previously posted Work Request (Send, Receive, RDMA Write, etc.).
12///
13/// # Structure
14///
15/// * **ID** — The `wr_id` allows you to map this completion back to the specific request posted.
16/// * **Result** — Indicates whether the operation succeeded or failed.
17#[derive(Copy, Clone, Debug)]
18pub struct WorkCompletion {
19    wr_id: u64,
20    result: WorkResult,
21}
22
23impl WorkCompletion {
24    pub(crate) fn new(wc: ibv_wc) -> Self {
25        Self {
26            wr_id: wc.wr_id(),
27            result: if let Some((error_code, vendor_code)) = wc.error() {
28                Err(WorkError::new(error_code, vendor_code))
29            } else {
30                Ok(WorkSuccess::new(wc.imm_data(), wc.len()))
31            },
32        }
33    }
34}
35
36impl WorkCompletion {
37    /// Returns the 64-bit identifier assigned to the original Work Request.
38    /// This value is passed through transparently by the hardware.
39    pub fn wr_id(&self) -> u64 {
40        self.wr_id
41    }
42
43    /// Returns the outcome of the operation.
44    ///
45    /// * `Ok(WorkSuccess)` — The operation completed successfully. Contains bytes transferred and immediate data.
46    /// * `Err(WorkError)` — The operation failed. Contains the error status and vendor syndrome.
47    pub fn result(&self) -> Result<WorkSuccess, WorkError> {
48        self.result
49    }
50}