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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! # Slices as C-structs.
//!
//! These slices are then transferable across the FFI boundary safely.

use core::convert::TryFrom;
use core::marker::PhantomData;
use std::prelude::v1::*;

/// Wrapper around const slices.
///
/// This is meant as a safe type to pass across the FFI boundary with similar semantics as regular
/// slice. However, not all functionality is present, use the slice conversion functions.
///
/// # Examples
///
/// Simple conversion:
///
/// ```
/// use cglue::slice::CSliceRef;
///
/// let arr = [0, 5, 3, 2];
///
/// let cslice = CSliceRef::from(&arr[..]);
///
/// let slice = cslice.as_slice();
///
/// assert_eq!(&arr, slice);
/// ```
#[repr(C)]
#[derive(Clone, Copy)]
#[cfg_attr(feature = "abi_stable", derive(::abi_stable::StableAbi))]
pub struct CSliceRef<'a, T: 'a> {
    data: *const T,
    len: usize,
    _lifetime: PhantomData<&'a T>,
}

unsafe impl<'a, T> Send for CSliceRef<'a, T> where T: Send {}
unsafe impl<'a, T> Sync for CSliceRef<'a, T> where T: Sync {}

impl<T: core::fmt::Debug> core::fmt::Debug for CSliceRef<'_, T>
where
    for<'a> &'a [T]: core::fmt::Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        f.debug_struct("CSliceRef")
            .field("data", &self.data)
            .field("len", &self.len)
            .field("slice", &self.as_slice());
        Ok(())
    }
}

impl core::fmt::Display for CSliceRef<'_, u8> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "{}", String::from_utf8_lossy(self.as_slice()))
    }
}

impl<'a, T> CSliceRef<'a, T> {
    /// Get the length of the slice.
    pub const fn len(&self) -> usize {
        self.len
    }

    /// Check if the slice is empty.
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the raw pointer to the slice.
    pub const fn as_ptr(&self) -> *const T {
        self.data
    }

    /// Reinterpret as a Rust slice.
    pub fn as_slice(&'a self) -> &'a [T] {
        unsafe { core::slice::from_raw_parts(self.data, self.len) }
    }

    /// Construct from a Rust slice.
    pub const fn from_slice(s: &'a [T]) -> Self {
        Self {
            data: s.as_ptr(),
            len: s.len(),
            _lifetime: PhantomData {},
        }
    }
}

impl<'a> CSliceRef<'a, u8> {
    /// Construct from a utf-8 string.
    pub const fn from_str(s: &'a str) -> Self {
        Self::from_slice(s.as_bytes())
    }
}

impl<'a> CSliceRef<'a, u8> {
    /// Convert into a utf-8 string.
    ///
    /// # Safety
    ///
    /// The slice must be a valid utf-8 string.
    pub unsafe fn into_str(self) -> &'a str {
        core::str::from_utf8_unchecked(core::slice::from_raw_parts(self.data, self.len))
    }
}

impl<'a> From<&'a str> for CSliceRef<'a, u8> {
    fn from(from: &'a str) -> Self {
        Self::from_str(from)
    }
}

impl<'a, T> From<&'a [T]> for CSliceRef<'a, T> {
    fn from(from: &'a [T]) -> Self {
        Self::from_slice(from)
    }
}

impl<'a, T> From<CSliceRef<'a, T>> for &'a [T] {
    fn from(from: CSliceRef<'a, T>) -> Self {
        unsafe { core::slice::from_raw_parts(from.data, from.len) }
    }
}

impl<'a> TryFrom<CSliceRef<'a, u8>> for &'a str {
    type Error = core::str::Utf8Error;

    fn try_from(from: CSliceRef<'a, u8>) -> Result<Self, Self::Error> {
        core::str::from_utf8(unsafe { core::slice::from_raw_parts(from.data, from.len) })
    }
}

impl<T> core::ops::Deref for CSliceRef<'_, T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

/// Wrapper around mutable slices.
///
/// This is meant as a safe type to pass across the FFI boundary with similar semantics as regular
/// slice. However, not all functionality is present, use the slice conversion functions.
#[repr(C)]
#[cfg_attr(feature = "abi_stable", derive(::abi_stable::StableAbi))]
pub struct CSliceMut<'a, T: 'a> {
    data: *mut T,
    len: usize,
    _lifetime: PhantomData<&'a T>,
}

unsafe impl<'a, T> Send for CSliceMut<'a, T> where T: Send {}
unsafe impl<'a, T> Sync for CSliceMut<'a, T> where T: Sync {}

impl<T: core::fmt::Debug> core::fmt::Debug for CSliceMut<'_, T>
where
    for<'a> &'a [T]: core::fmt::Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
        f.debug_struct("CSliceMut")
            .field("data", &self.data)
            .field("len", &self.len)
            .field("slice", &self.as_slice());
        Ok(())
    }
}

impl core::fmt::Display for CSliceMut<'_, u8> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "{}", String::from_utf8_lossy(self.as_slice()))
    }
}

impl<'a, T> CSliceMut<'a, T> {
    /// Get the length of the slice.
    pub const fn len(&self) -> usize {
        self.len
    }

    /// Check if the slice is empty.
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get constant pointer to the underlying data.
    pub const fn as_ptr(&self) -> *const T {
        self.data as *const T
    }

    /// Get mutable pointer to the underlying data.
    pub const fn as_mut_ptr(&self) -> *mut T {
        self.data
    }

    /// Reinterpret as a const Rust slice.
    pub fn as_slice(&'a self) -> &'a [T] {
        unsafe { core::slice::from_raw_parts(self.data, self.len) }
    }

    /// Reinterpret as a mutable Rust slice.
    pub fn as_slice_mut(&'a mut self) -> &'a mut [T] {
        unsafe { core::slice::from_raw_parts_mut(self.data, self.len) }
    }
}

impl<'a> CSliceMut<'a, u8> {
    /// Convert into utf-8 string
    ///
    /// # Safety
    ///
    /// The underlying slice must be a valid utf-8 string.
    pub unsafe fn into_str(self) -> &'a str {
        core::str::from_utf8_unchecked(core::slice::from_raw_parts(self.data, self.len))
    }

    /// Convert into mutable utf-8 string
    ///
    /// # Safety
    ///
    /// The underlying slice must be a valid utf-8 string.
    pub unsafe fn into_mut_str(self) -> &'a mut str {
        core::str::from_utf8_unchecked_mut(core::slice::from_raw_parts_mut(self.data, self.len))
    }
}

impl<'a, T> From<&'a mut [T]> for CSliceMut<'a, T> {
    fn from(from: &'a mut [T]) -> Self {
        Self {
            data: from.as_mut_ptr(),
            len: from.len(),
            _lifetime: PhantomData::default(),
        }
    }
}

impl<'a> From<&'a mut str> for CSliceMut<'a, u8> {
    fn from(from: &'a mut str) -> Self {
        Self {
            data: from.as_mut_ptr(),
            len: from.len(),
            _lifetime: PhantomData::default(),
        }
    }
}

impl<'a, T> From<CSliceMut<'a, T>> for &'a [T] {
    fn from(from: CSliceMut<'a, T>) -> Self {
        unsafe { core::slice::from_raw_parts(from.data, from.len) }
    }
}

impl<'a: 'b, 'b, T> From<&'b CSliceMut<'a, T>> for CSliceMut<'a, T> {
    fn from(from: &'b CSliceMut<'a, T>) -> Self {
        Self {
            data: from.data,
            len: from.len,
            _lifetime: from._lifetime,
        }
    }
}

impl<'a: 'b, 'b, T> From<&'b mut CSliceMut<'a, T>> for CSliceMut<'a, T> {
    fn from(from: &'b mut CSliceMut<'a, T>) -> Self {
        Self {
            data: from.data,
            len: from.len,
            _lifetime: from._lifetime,
        }
    }
}

impl<'a> TryFrom<CSliceMut<'a, u8>> for &'a str {
    type Error = core::str::Utf8Error;

    fn try_from(from: CSliceMut<'a, u8>) -> Result<Self, Self::Error> {
        core::str::from_utf8(unsafe { core::slice::from_raw_parts(from.data, from.len) })
    }
}

impl<'a, T> From<CSliceMut<'a, T>> for &'a mut [T] {
    fn from(from: CSliceMut<'a, T>) -> Self {
        unsafe { core::slice::from_raw_parts_mut(from.data, from.len) }
    }
}

impl<'a> TryFrom<CSliceMut<'a, u8>> for &'a mut str {
    type Error = core::str::Utf8Error;

    fn try_from(from: CSliceMut<'a, u8>) -> Result<Self, Self::Error> {
        core::str::from_utf8_mut(unsafe { core::slice::from_raw_parts_mut(from.data, from.len) })
    }
}

impl<'a, T> core::ops::Deref for CSliceMut<'a, T> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<'a, T> core::ops::DerefMut for CSliceMut<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { core::slice::from_raw_parts_mut(self.data, self.len) }
    }
}