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
//! Low level interface to C-API's that expect a pointer/size reference to a buffer
//!
//! In many libc API's it is common to pass a pointer/size pair into a function.
//! This describes the location and length of data to be read or written.
//!
//! This library provides tools to generate such pointer/size pairs in rust.  In rust these
//! are either vectors or array of 'u8' values.  Buffers used for reading data in can be
//! defined as 'uninit_array!' to reduce the overhead of unnnecessary initialization.
//!
#![feature(maybe_uninit_slice)]
use libc::{c_char, c_void, size_t};
use std::mem::MaybeUninit;
use std::vec::Vec;

/// The 'TxBuffer' is a used when writing data out.
/// It's contents must be fully initialized.
pub trait TxBuffer {
    /// The used length
    fn len(&self) -> usize;

    /// Returns a const void*/size_t pair to be used in the C call.
    fn as_c_void(&self) -> (*const c_void, size_t);

    /// Returns a const char*/size_t pair to be used in the C call.
    fn as_c_char(&self) -> (*const c_char, size_t);
}

impl TxBuffer for Vec<u8> {
    fn len(&self) -> usize {
        self.len()
    }

    fn as_c_void(&self) -> (*const c_void, size_t) {
        (self.as_ptr() as *mut c_void, self.len())
    }

    fn as_c_char(&self) -> (*const c_char, size_t) {
        (self.as_ptr() as *mut c_char, self.len())
    }
}

impl<const N: usize> TxBuffer for [u8; N] {
    fn len(&self) -> usize {
        N
    }

    fn as_c_void(&self) -> (*const c_void, size_t) {
        (self.as_ptr() as *mut c_void, self.len())
    }

    fn as_c_char(&self) -> (*const c_char, size_t) {
        (self.as_ptr() as *mut c_char, self.len())
    }

}

/// The 'RxBuffer' is a used when reading data in.
/// The contents of the buffer can be uninitialized.
pub trait RxBuffer {
    /// The allocated size
    fn capacity(&self) -> usize;

    /// Returns a void*/size_t pair to be used in the C call.
    fn as_c_void(&mut self) -> (*mut c_void, size_t);

    /// Returns a char*/size_t pair to be used in the C call.
    fn as_c_char(&mut self) -> (*mut c_char, size_t);

    /// After the reading operation is done the buffer must be sealed
    /// with the actual length of the data retrieved.
    /// This function returns a slice into the buffer containing the
    /// data.
    unsafe fn done(&mut self, len: size_t) -> &[u8];

    /// Get a 'Some(ResizeableBuffer)' when the underlying Buffer supports allocating more memory
    #[inline]
    fn can_set_capacity(&mut self) -> Option<&mut dyn ResizeableBuffer> {
        None
    }
}

impl RxBuffer for Vec<u8> {
    fn capacity(&self) -> usize {
        self.capacity()
    }

    fn as_c_void(&mut self) -> (*mut c_void, size_t) {
        (self.as_mut_ptr() as *mut c_void, self.capacity())
    }

    fn as_c_char(&mut self) -> (*mut c_char, size_t) {
        (self.as_mut_ptr() as *mut c_char, self.capacity())
    }

    unsafe fn done(&mut self, len: usize) -> &[u8] {
        assert!(len <= self.capacity());
        self.set_len(len);
        &self[..len]
    }

    #[inline]
    fn can_set_capacity(&mut self) -> Option<&mut dyn ResizeableBuffer> {
        Some(self)
    }
}


impl<const N: usize> RxBuffer for [u8; N] {
    fn capacity(&self) -> usize {
        N
    }

    fn as_c_void(&mut self) -> (*mut c_void, size_t) {
        (self.as_mut_ptr() as *mut c_void, N)
    }

    fn as_c_char(&mut self) -> (*mut c_char, size_t) {
        (self.as_mut_ptr() as *mut c_char, N)
    }

    unsafe fn done(&mut self, len: usize) -> &[u8] {
        &self[..len]
    }
}

impl<const N: usize> RxBuffer for [MaybeUninit<u8>; N] {
    fn capacity(&self) -> usize {
        N
    }

    fn as_c_void(&mut self) -> (*mut c_void, size_t) {
        (self.as_mut_ptr() as *mut c_void, N)
    }

    fn as_c_char(&mut self) -> (*mut c_char, size_t) {
        (self.as_mut_ptr() as *mut c_char, N)
    }

    unsafe fn done(&mut self, len: usize) -> &[u8] {
        MaybeUninit::slice_assume_init_ref(&self[..len])
    }
}

/// Implemented for Buffers which can be resized
pub trait ResizeableBuffer {
    /// Change buffer size (allocate more memory)
    fn set_capacity(&mut self, len: usize);
}

impl ResizeableBuffer for Vec<u8> {
    fn set_capacity(&mut self, len: usize) {
        // grow only
        if self.capacity() < len {
            self.reserve(len - self.len());
        };
    }
}