| 1 | //! IoVec extensions for Unix platforms. |
| 2 | //! |
| 3 | //! These functions provide conversions to unix specific representations of the |
| 4 | //! vectored data. |
| 5 | //! |
| 6 | //! # Examples |
| 7 | //! |
| 8 | //! ``` |
| 9 | //! use iovec::IoVec; |
| 10 | //! use iovec::unix; |
| 11 | //! |
| 12 | //! let a = b"hello".to_vec(); |
| 13 | //! let b = b"world".to_vec(); |
| 14 | //! |
| 15 | //! let bufs: &[&IoVec] = &[(&a[..]).into(), (&b[..]).into()]; |
| 16 | //! let os_bufs = unix::as_os_slice(&bufs[..]); |
| 17 | //! |
| 18 | //! // Use the `os_bufs` slice with `writev`. |
| 19 | //! ``` |
| 20 | |
| 21 | use IoVec; |
| 22 | use libc; |
| 23 | |
| 24 | use std::mem; |
| 25 | |
| 26 | /// Convert a slice of `IoVec` refs to a slice of `libc::iovec`. |
| 27 | /// |
| 28 | /// The return value can be passed to `writev` bindings. |
| 29 | /// |
| 30 | /// # Examples |
| 31 | /// |
| 32 | /// ``` |
| 33 | /// use iovec::IoVec; |
| 34 | /// use iovec::unix; |
| 35 | /// |
| 36 | /// let a = b"hello".to_vec(); |
| 37 | /// let b = b"world".to_vec(); |
| 38 | /// |
| 39 | /// let bufs: &[&IoVec] = &[a[..].into(), b[..].into()]; |
| 40 | /// let os_bufs = unix::as_os_slice(bufs); |
| 41 | /// |
| 42 | /// // Use the `os_bufs` slice with `writev`. |
| 43 | /// ``` |
| 44 | pub fn as_os_slice<'a>(iov: &'a [&IoVec]) -> &'a [libc::iovec] { |
| 45 | unsafe { mem::transmute(iov) } |
| 46 | } |
| 47 | |
| 48 | /// Convert a mutable slice of `IoVec` refs to a mutable slice of `libc::iovec`. |
| 49 | /// |
| 50 | /// The return value can be passed to `readv` bindings. |
| 51 | /// |
| 52 | /// # Examples |
| 53 | /// |
| 54 | /// ``` |
| 55 | /// use iovec::IoVec; |
| 56 | /// use iovec::unix; |
| 57 | /// |
| 58 | /// let mut a = [0; 10]; |
| 59 | /// let mut b = [0; 10]; |
| 60 | /// |
| 61 | /// let bufs: &mut [&mut IoVec] = &mut [(&mut a[..]).into(), (&mut b[..]).into()]; |
| 62 | /// let os_bufs = unix::as_os_slice_mut(bufs); |
| 63 | /// |
| 64 | /// // Use the `os_bufs` slice with `readv`. |
| 65 | /// ``` |
| 66 | pub fn as_os_slice_mut<'a>(iov: &'a mut [&mut IoVec]) -> &'a mut [libc::iovec] { |
| 67 | unsafe { mem::transmute(iov) } |
| 68 | } |