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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#![no_std]
#![feature(raw, unboxed_closures)]
/*!
Provides the `DynSized` trait, which allows conversion between fat pointers and their (meta, data_pointer) components. `derive_DynSized!` may be used to implement `DynSized` for trait objects.
*/


extern crate fn_move;

use core::{str, slice, ptr};
use core::raw;
#[doc(hidden)]
pub use core::{mem};

/// A trait for dynamically sized types, similar in principle to the `Sized`
/// trait. Allows conversion between fat and thin pointers.
///
/// The assemble and disassemble methods must be safe, i.e. they must not dereference the raw pointers, only use them to extract the metadata in the case of `disassemble`, or to combine with metadata to produce a fat pointer, in the case of `assemble`.
///
/// # Unsafety
///
/// The trait is marked `unsafe`, because implementing it wrong can cause undefined behaviour.
pub unsafe trait DynSized {
    type Meta: Copy;

    fn assemble(meta: Self::Meta, data: *const ()) -> *const Self;

    fn assemble_mut(meta: Self::Meta, data: *mut ()) -> *mut Self {
        unsafe {
            // transmute is safe here, because we're just changing from
            // *const Self to *mut Self
            mem::transmute(Self::assemble(meta, data))
        }
    }

    fn disassemble(ptr: *const Self) -> (Self::Meta, *const ());

    fn disassemble_mut(ptr: *mut Self) -> (Self::Meta, *mut ()) {
        let (meta, data) = Self::disassemble(ptr);
        unsafe {
            (meta, mem::transmute(data))
        }
    }

    fn meta(&self) -> Self::Meta {
        let (meta, _) = Self::disassemble(self);
        meta
    }

    fn data(&self) -> *const () {
        let (_, data) = Self::disassemble(self);
        data
    }

    fn data_mut(&mut self) -> *mut () {
        let (_, data) = Self::disassemble_mut(self);
        data
    }
}

/// A version of mem::size_of_val that requires only the pointer metadata
pub fn size_of_val<T>(meta: T::Meta) -> usize where
    T: DynSized + ?Sized
{
    unsafe {  mem::size_of_val(&*T::assemble(meta, ptr::null())) }
}

/// A version of mem::align_of_val that requires only the pointer metadata
pub fn align_of_val<T>(meta: T::Meta) -> usize where
    T: DynSized + ?Sized
{
    unsafe {  mem::align_of_val(&*T::assemble(meta, ptr::null())) }
}

/// A wrapper type for `Sized` types that implements `DynSized`.
/// 
/// This is unfortunately necessary because a blanket `impl` of `DynSized` for all `Sized` types would conflict with implementations for user-defined structs that are ?Sized.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct WrapSized<T>(pub T);

unsafe impl<T> DynSized for WrapSized<T> {
    type Meta = ();

    fn assemble(_: (), data: *const ()) -> *const WrapSized<T> {
        data as *const WrapSized<T>
    }

    fn disassemble(ptr: *const WrapSized<T>) -> ((), *const ()) {
        ((), ptr as *const ())
    }
}

unsafe impl<T> DynSized for [T] {
    type Meta = usize;

    fn assemble(len: usize, data: *const ()) -> *const [T] {
        unsafe {
            slice::from_raw_parts(data as *const T, len)
        }
    }

    fn disassemble(slice: *const [T]) -> (usize, *const ()) {
        let slice = unsafe { &*slice };
        (slice.len(), slice.as_ptr() as *const ())
    }
}

#[test]
fn test_slice() {
    let slice = &[1,2,3] as &[i32];
    let (len, ptr) = DynSized::disassemble(slice);
    let new_slice: &[i32] = unsafe {
        &*DynSized::assemble(len, ptr)
    };
    assert_eq!(new_slice, slice);
}

unsafe impl DynSized for str {
    type Meta = usize;

    fn assemble(len: usize, data: *const ()) -> *const str {
        unsafe {
            str::from_utf8_unchecked(slice::from_raw_parts(data as *const u8, len))
        }
    }

    fn disassemble(s: *const str) -> (usize, *const ()) {
        unsafe {
            DynSized::disassemble((&*s).as_bytes())
        }
    }
}

#[test]
fn test_str() {
    let s = "Yolo123";
    let (ptr, len) = DynSized::disassemble(s);
    let new_s: &str = unsafe {
        &*DynSized::assemble(ptr, len)
    };
    assert_eq!(s, new_s);
}

#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct TraitObject(raw::TraitObject);

#[derive(Copy, Clone, Debug)]
#[doc(hidden)]
pub struct Vtable(*mut ());

impl TraitObject {
    pub fn construct(vtable: Vtable, data: *mut ()) -> TraitObject {
        TraitObject(raw::TraitObject {
            data: data,
            vtable: vtable.0,
        })
    }

    pub fn data(self) -> *mut () {
        self.0.data
    }

    pub fn vtable(self) -> Vtable {
        Vtable(self.0.vtable)
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! __derive_DynSized_body {
    ($Trait:ty) => {
        type Meta = $crate::Vtable;

        fn assemble(vtable: $crate::Vtable, data: *const ()) -> *const Self {
            unsafe {
                $crate::mem::transmute(
                    $crate::TraitObject::construct(vtable, data as *mut ())
                )
            }
        }

        fn disassemble(ptr: *const Self) -> (Self::Meta, *const ()) {
            let trait_object: $crate::TraitObject = unsafe {
                $crate::mem::transmute(ptr)
            };

            (trait_object.vtable(), trait_object.data())
        }
    };
}

/// Derives the `DynSized` trait for trait objects.
///
/// To use:
///
/// ```
/// #[macro_use] extern crate dyn_sized;
/// # fn main() {
/// trait MyTrait {}
/// derive_DynSized!(MyTrait);
///
/// trait MyGenericTrait<'a, T: 'a> {
///     fn foo(&'a self) -> T;
/// }
/// // type arguments for the impl go after the trait object type.
/// derive_DynSized!(MyGenericTrait<'a, T>, 'a, T: 'a);
/// # }
/// ```
///
#[macro_export]
macro_rules! derive_DynSized {
    ($Trait:ty) => {
        unsafe impl $crate::DynSized for $Trait {
            __derive_DynSized_body!($Trait);
        }
    };

    ($Trait:ty, $($args:tt)+ ) => {
        unsafe impl<$($args)+> $crate::DynSized for $Trait {
            __derive_DynSized_body!($Trait);
        }
    };
}

use core::any::Any;
use fn_move::FnMove;

derive_DynSized!(Any);
derive_DynSized!(Any + Send);
derive_DynSized!(Fn<Args, Output=Output> + 'a, 'a, Args, Output);
derive_DynSized!(Fn<Args, Output=Output> + Send + 'a, 'a, Args, Output);
derive_DynSized!(Fn<Args, Output=Output> + Sync + 'a, 'a, Args, Output);
derive_DynSized!(Fn<Args, Output=Output> + Send + Sync + 'a, 'a, Args, Output);
derive_DynSized!(FnMut<Args, Output=Output> + 'a, 'a, Args, Output);
derive_DynSized!(FnMut<Args, Output=Output> + Send + 'a, 'a, Args, Output);
derive_DynSized!(FnOnce<Args, Output=Output> + 'a, 'a, Args, Output);
derive_DynSized!(FnOnce<Args, Output=Output> + Send + 'a, 'a, Args, Output);
derive_DynSized!(FnMove<Args, Output=Output> + 'a, 'a, Args, Output);
derive_DynSized!(FnMove<Args, Output=Output> + Send + 'a, 'a, Args, Output);

#[test]
#[allow(non_snake_case)]
fn test_derive_DynSized() {
    use core::borrow::Borrow;
    trait MyBorrow<Borrowed>: Borrow<Borrowed> {}
    derive_DynSized!(MyBorrow<Borrowed>, Borrowed);
}

/// An extension trait adding .meta() and .data() convenience methods
/// to built-in pointer types
pub trait PtrExt {
    type Referent: DynSized + ?Sized;

    fn meta(&self) -> <Self::Referent as DynSized>::Meta;

    fn data(&self) -> *const ();
}

/// adds the `data_mut` method to `*mut T`
pub trait PtrMutExt: PtrExt {
    fn data_mut(&self) -> *mut ();
}

impl<T: DynSized + ?Sized> PtrExt for *const T {
    type Referent = T;

    fn meta(&self) -> T::Meta  {
        let (meta, _) = T::disassemble(*self);
        meta
    }

    fn data(&self) -> *const () {
        let (_, data) = T::disassemble(*self);
        data
    }
}

impl<T: DynSized + ?Sized> PtrExt for *mut T {
    type Referent = T;

    fn meta(&self) -> T::Meta  {
        (*self as *const T).meta()
    }

    fn data(&self) -> *const () {
        let (_, data) = T::disassemble(*self);
        data
    }
}

impl<T: DynSized + ?Sized> PtrMutExt for *mut T {

    fn data_mut(&self) -> *mut () {
        let (_, data) = T::disassemble_mut(*self);
        data
    }
}

#[test]
#[allow(non_snake_case)]
fn test_PtrExt() {
    let slice: &mut [i32] = &mut [1,2,3];

    let len: <[i32] as DynSized>::Meta = slice.meta();
    assert_eq!(len, 3usize);
    let len: <[i32] as DynSized>::Meta = (slice as &[i32]).meta();
    assert_eq!(len, 3usize);
    let len: <[i32] as DynSized>::Meta = (slice as *mut [i32]).meta();
    assert_eq!(len, 3usize);
    let len: <[i32] as DynSized>::Meta = (slice as *const [i32]).meta();
    assert_eq!(len, 3usize);

    let data: *const () = slice.data();
    assert_eq!(slice as *const [_] as *const (), data);
    let data: *const () = (slice as &[i32]).data();
    assert_eq!(slice as *const [_] as *const (), data);
    let data: *const () = (slice as *const [i32]).data();
    assert_eq!(slice as *const [_] as *const (), data);
    let data: *const () = (slice as *mut [i32]).data();
    assert_eq!(slice as *const [_] as *const (), data);

    let data: *mut () = slice.data_mut();
    assert_eq!(slice as *mut [_] as *mut (), data);
    let data: *mut () = (slice as *mut [i32]).data_mut();
    assert_eq!(slice as *mut [_] as *mut (), data);
}