summaryrefslogtreecommitdiff
path: root/monoio/src/buf/raw_buf.rs
blob: 912bc4094108d311bcd298be1811478995b0070a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::ptr::null;

#[cfg(windows)]
use windows_sys::Win32::Networking::WinSock::WSABUF;

use super::{IoBuf, IoBufMut, IoVecBuf, IoVecBufMut};

/// RawBuf is not a real buf. It only hold the pointer of the buffer.
/// Users must make sure the buffer behind the pointer is always valid.
/// Which means, user must:
/// 1. await the future with RawBuf Ready before drop the real buffer
/// 2. make sure the pointer and length is valid before the future Ready
pub struct RawBuf {
    ptr: *const u8,
    len: usize,
}

impl RawBuf {
    /// Create a empty RawBuf.
    /// # Safety
    /// do not use uninitialized RawBuf directly.
    #[inline]
    pub unsafe fn uninit() -> Self {
        Self {
            ptr: null(),
            len: 0,
        }
    }

    /// Create a new RawBuf with given pointer and length.
    /// # Safety
    /// make sure the pointer and length is valid when RawBuf is used.
    #[inline]
    pub unsafe fn new(ptr: *const u8, len: usize) -> Self {
        Self { ptr, len }
    }
}

unsafe impl IoBuf for RawBuf {
    #[inline]
    fn read_ptr(&self) -> *const u8 {
        self.ptr
    }

    #[inline]
    fn bytes_init(&self) -> usize {
        self.len
    }
}

unsafe impl IoBufMut for RawBuf {
    #[inline]
    fn write_ptr(&mut self) -> *mut u8 {
        self.ptr as *mut u8
    }

    #[inline]
    fn bytes_total(&mut self) -> usize {
        self.len
    }

    #[inline]
    unsafe fn set_init(&mut self, _pos: usize) {}
}

impl RawBuf {
    /// Create a new RawBuf with the first iovec part.
    /// # Safety
    /// make sure the pointer and length is valid when RawBuf is used.
    #[inline]
    pub unsafe fn new_from_iovec_mut<T: IoVecBufMut>(data: &mut T) -> Option<Self> {
        if data.write_iovec_len() == 0 {
            return None;
        }
        #[cfg(unix)]
        {
            let iovec = *data.write_iovec_ptr();
            Some(Self::new(iovec.iov_base as *const u8, iovec.iov_len))
        }
        #[cfg(windows)]
        {
            let wsabuf = *data.write_wsabuf_ptr();
            Some(Self::new(wsabuf.buf as *const u8, wsabuf.len))
        }
    }

    /// Create a new RawBuf with the first iovec part.
    /// # Safety
    /// make sure the pointer and length is valid when RawBuf is used.
    #[inline]
    pub unsafe fn new_from_iovec<T: IoVecBuf>(data: &T) -> Option<Self> {
        #[cfg(unix)]
        {
            if data.read_iovec_len() == 0 {
                return None;
            }
            let iovec = *data.read_iovec_ptr();
            Some(Self::new(iovec.iov_base as *const u8, iovec.iov_len))
        }
        #[cfg(windows)]
        {
            if data.read_wsabuf_len() == 0 {
                return None;
            }
            let wsabuf = *data.read_wsabuf_ptr();
            Some(Self::new(wsabuf.buf as *const u8, wsabuf.len))
        }
    }
}

/// RawBufVectored behaves like RawBuf.
/// And user must obey the following restrictions:
/// 1. await the future with RawBuf Ready before drop the real buffer
/// 2. make sure the pointer and length is valid before the future Ready
pub struct RawBufVectored {
    #[cfg(unix)]
    ptr: *const libc::iovec,
    #[cfg(windows)]
    ptr: *const WSABUF,
    len: usize,
}

impl RawBufVectored {
    /// Create a new RawBuf with given pointer and length.
    /// # Safety
    /// make sure the pointer and length is valid when RawBuf is used.
    #[cfg(unix)]
    #[inline]
    pub unsafe fn new(ptr: *const libc::iovec, len: usize) -> Self {
        Self { ptr, len }
    }
    #[cfg(windows)]
    #[inline]
    pub unsafe fn new(ptr: *const WSABUF, len: usize) -> Self {
        Self { ptr, len }
    }
}

unsafe impl IoVecBuf for RawBufVectored {
    #[cfg(unix)]
    #[inline]
    fn read_iovec_ptr(&self) -> *const libc::iovec {
        self.ptr
    }

    #[cfg(unix)]
    #[inline]
    fn read_iovec_len(&self) -> usize {
        self.len
    }

    #[cfg(windows)]
    #[inline]
    fn read_wsabuf_ptr(&self) -> *const WSABUF {
        self.ptr
    }

    #[cfg(windows)]
    #[inline]
    fn read_wsabuf_len(&self) -> usize {
        self.len
    }
}

unsafe impl IoVecBufMut for RawBufVectored {
    #[cfg(unix)]
    fn write_iovec_ptr(&mut self) -> *mut libc::iovec {
        self.ptr as *mut libc::iovec
    }

    #[cfg(unix)]
    fn write_iovec_len(&mut self) -> usize {
        self.len
    }

    #[cfg(windows)]
    #[inline]
    fn write_wsabuf_ptr(&self) -> *mut WSABUF {
        self.ptr as *mut WSABUF
    }

    #[cfg(windows)]
    #[inline]
    fn write_wsabuf_len(&self) -> usize {
        self.len
    }

    unsafe fn set_init(&mut self, _pos: usize) {}
}