Skip to main content

dope_uring/raw/
open.rs

1use std::os::fd;
2
3use dope_sys::{
4    Errno,
5    io::{
6        file,
7        reactor::complete::{Decode, Outcome as _},
8    },
9};
10
11use crate::{
12    Backend,
13    reactor::cq::{Invalid, Outcome},
14};
15
16// SAFETY: nonnegative native open results uniquely transfer their descriptor;
17// the returned OwnedFd delivers or closes it exactly once.
18unsafe impl file::raw::Completion for Backend {
19    type Outcome = Outcome;
20    type Invalid = Invalid;
21
22    unsafe fn open(raw: Self::Outcome) -> Result<Result<fd::OwnedFd, Errno>, Self::Invalid> {
23        match raw.decode() {
24            Decode::Value(value) => {
25                // SAFETY: the caller owns this successful open completion exactly once.
26                let owned =
27                    unsafe { <fd::OwnedFd as fd::FromRawFd>::from_raw_fd(value.get() as i32) };
28                Ok(Ok(owned))
29            }
30            Decode::Error(code) => Ok(Err(code)),
31            Decode::Invalid(invalid) => Err(invalid),
32        }
33    }
34}