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};
11
12use crate::{
13    Uring,
14    reactor::cq::{Invalid, Outcome},
15};
16
17// SAFETY: nonnegative native open results uniquely transfer their descriptor;
18// the returned OwnedFd delivers or closes it exactly once.
19unsafe impl file::raw::Completion for Uring {
20    type Outcome = Outcome;
21    type Invalid = Invalid;
22
23    unsafe fn open(raw: Self::Outcome) -> Result<file::Opened, Self::Invalid> {
24        match raw.decode() {
25            Decode::Value(value) => {
26                // SAFETY: the caller owns this successful open completion exactly once.
27                let owned =
28                    unsafe { <fd::OwnedFd as fd::FromRawFd>::from_raw_fd(value.get() as i32) };
29                Ok(Ok(owned))
30            }
31            Decode::Error(code) => Ok(Err(code)),
32            Decode::Invalid(invalid) => Err(invalid),
33        }
34    }
35
36    fn close(file: fd::OwnedFd) -> Result<(), Errno> {
37        <Uring as Fd>::close(file)
38    }
39}