Skip to main content

dope_uring/raw/
open.rs

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