Skip to main content

dope_uring/driver/buf/
fixed.rs

1use super::super::resources::ResourceToken;
2
3pub struct FixedBufGuard {
4    token: Option<ResourceToken>,
5    buf_index: u16,
6    ptr: *mut u8,
7    cap: usize,
8}
9
10impl FixedBufGuard {
11    pub(in crate::driver) fn new(
12        token: ResourceToken,
13        buf_index: u16,
14        ptr: *mut u8,
15        cap: usize,
16    ) -> Self {
17        Self {
18            token: Some(token),
19            buf_index,
20            ptr,
21            cap,
22        }
23    }
24
25    pub fn fixed_write(&self, src: &[u8]) -> FixedBufWrite {
26        let len = src.len().min(self.cap);
27        if len != 0 {
28            unsafe {
29                std::ptr::copy_nonoverlapping(src.as_ptr(), self.ptr, len);
30            }
31        }
32        FixedBufWrite::new(self.ptr as *const u8, len as u32, self.buf_index)
33    }
34}
35
36impl Drop for FixedBufGuard {
37    fn drop(&mut self) {
38        if let Some(token) = self.token.take() {
39            token.release();
40        }
41    }
42}
43
44#[derive(Clone, Copy, Debug)]
45pub struct FixedBufWrite {
46    ptr: *const u8,
47    len: u32,
48    buf_index: u16,
49}
50
51impl FixedBufWrite {
52    #[must_use]
53    pub(crate) const fn new(ptr: *const u8, len: u32, buf_index: u16) -> Self {
54        Self {
55            ptr,
56            len,
57            buf_index,
58        }
59    }
60
61    pub const fn ptr(self) -> *const u8 {
62        self.ptr
63    }
64
65    pub const fn len(self) -> u32 {
66        self.len
67    }
68
69    pub const fn index(self) -> u16 {
70        self.buf_index
71    }
72}