Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! A specialized byte slice type for performing vectored I/O operations.
2 //!
3 //! For more detail, see [`IoVec`] documentation.
4 //!
5 //! [`IoVec`]: struct.IoVec.html
6
7 #[cfg(unix)]
8 extern crate libc;
9
10 mod sys;
11
12 use std::{ops, mem};
13
14 #[cfg(unix)]
15 pub mod unix;
16
17 /// Max length of an `IoVec` slice.
18 ///
19 /// Attempts to convert slices longer than this value will result in a panic.
20 pub const MAX_LENGTH: usize = sys::MAX_LENGTH;
21
22 /// A specialized byte slice type for performing vectored I/O operations.
23 ///
24 /// On all systems, the types needed to perform vectored I/O systems have the
25 /// same size as Rust's [`slice`]. However, the layout is not necessarily the
26 /// same. `IoVec` provides a portable compatibility layer.
27 ///
28 /// The `IoVec` behaves like a Rust [`slice`], providing the same functions.
29 /// It also provides conversion functions to and from the OS specific vectored
30 /// types.
31 ///
32 /// [`slice`]: https://doc.rust-lang.org/std/primitive.slice.html
33 ///
34 /// # Examples
35 ///
36 /// ```
37 /// use iovec::IoVec;
38 ///
39 /// let mut data = vec![];
40 /// data.extend_from_slice(b"hello");
41 ///
42 /// let iovec: &IoVec = data.as_slice().into();
43 ///
44 /// assert_eq!(&iovec[..], &b"hello"[..]);
45 /// ```
46 ///
47 /// # Panics
48 ///
49 /// Attempting to convert a zero-length slice or a slice longer than
50 /// [`MAX_LENGTH`] to an `IoVec` will result in a panic.
51 ///
52 /// [`MAX_LENGTH`]: constant.MAX_LENGTH.html
53 pub struct IoVec {
54 sys: sys::IoVec,
55 }
56
57 impl IoVec {
58 pub fn from_bytes(slice: &[u8]) -> Option<&IoVec> {
59 if slice.len() == 0 {
60 return None
61 }
62 unsafe {
63 let iovec: &sys::IoVec = slice.into();
64 Some(mem::transmute(iovec))
65 }
66 }
67
68 pub fn from_bytes_mut(slice: &mut [u8]) -> Option<&mut IoVec> {
69 if slice.len() == 0 {
70 return None
71 }
72 unsafe {
73 let iovec: &mut sys::IoVec = slice.into();
74 Some(mem::transmute(iovec))
75 }
76 }
77
78 #[deprecated(since = "0.1.0", note = "deref instead")]
79 #[doc(hidden)]
80 pub fn as_bytes(&self) -> &[u8] {
81 &**self
82 }
83
84 #[deprecated(since = "0.1.0", note = "deref instead")]
85 #[doc(hidden)]
86 pub fn as_mut_bytes(&mut self) -> &mut [u8] {
87 &mut **self
88 }
89 }
90
91 impl ops::Deref for IoVec {
92 type Target = [u8];
93
94 fn deref(&self) -> &[u8] {
95 &self.sys.as_ref()
96 }
97 }
98
99 impl ops::DerefMut for IoVec {
100 fn deref_mut(&mut self) -> &mut [u8] {
101 self.sys.as_mut()
102 }
103 }
104
105 #[doc(hidden)]
106 impl<'a> From<&'a [u8]> for &'a IoVec {
107 fn from(bytes: &'a [u8]) -> &'a IoVec {
108 IoVec::from_bytes(bytes)
109 .expect("this crate accidentally accepted 0-sized slices \
110 originally but this was since discovered as a soundness \
111 hole, it's recommended to use the `from_bytes` \
112 function instead")
113 }
114 }
115
116 #[doc(hidden)]
117 impl<'a> From<&'a mut [u8]> for &'a mut IoVec {
118 fn from(bytes: &'a mut [u8]) -> &'a mut IoVec {
119 IoVec::from_bytes_mut(bytes)
120 .expect("this crate accidentally accepted 0-sized slices \
121 originally but this was since discovered as a soundness \
122 hole, it's recommended to use the `from_bytes_mut` \
123 function instead")
124 }
125 }
126
127 #[doc(hidden)]
128 impl<'a> Default for &'a IoVec {
129 fn default() -> Self {
130 panic!("this implementation was accidentally provided but is \
131 unfortunately unsound, it's recommended to stop using \
132 `IoVec::default` or construct a vector with a nonzero length");
133 }
134 }
135
136 #[doc(hidden)]
137 impl<'a> Default for &'a mut IoVec {
138 fn default() -> Self {
139 panic!("this implementation was accidentally provided but is \
140 unfortunately unsound, it's recommended to stop using \
141 `IoVec::default` or construct a vector with a nonzero length");
142 }
143 }
144
145 #[cfg(test)]
146 mod test {
147 use super::IoVec;
148
149 #[test]
150 fn convert_ref() {
151 let buf: &IoVec = (&b"hello world"[..]).into();
152 assert_eq!(buf[..], b"hello world"[..]);
153 }
154
155 #[test]
156 fn convert_mut() {
157 let mut buf: Vec<u8> = b"hello world".to_vec();
158 let buf: &mut IoVec = (&mut buf[..]).into();
159 assert_eq!(buf[..], b"hello world"[..]);
160 }
161 }