Skip to main content

dope_uring/raw/
open.rs

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