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
use Buffer;
use BufferRef;
use ToBufferRef;
use std::mem;
use wildly_unsafe;

/// The intermediate step from a byte slice reference to a `BufferRef`.
pub struct SliceRefBuffer<'data> {
    slice: &'data mut &'data mut [u8],
    initialized: usize,
}

impl<'d> SliceRefBuffer<'d> {
    fn new(slice: &'d mut &'d mut [u8]) -> SliceRefBuffer<'d> {
        SliceRefBuffer {
            slice: slice,
            initialized: 0,
        }
    }
    fn buffer<'s>(&'s mut self) -> BufferRef<'d, 's> {
        unsafe {
            // This is unsafe, we now have two unique (mutable) references to
            // the same `Vec`. However, we will only access `self.vec.len`
            // through `self` and only the contents through the `BufferRef`.
            BufferRef::new(wildly_unsafe(self.slice), &mut self.initialized)
        }
    }
}

impl<'d> Drop for SliceRefBuffer<'d> {
    fn drop(&mut self) {
        let slice = mem::replace(self.slice, &mut []);
        *self.slice = &mut slice[..self.initialized];
    }
}

impl<'d> Buffer<'d> for &'d mut &'d mut [u8] {
    type Intermediate = SliceRefBuffer<'d>;
    fn to_to_buffer_ref(self) -> Self::Intermediate {
        SliceRefBuffer::new(self)
    }
}

impl<'d> ToBufferRef<'d> for SliceRefBuffer<'d> {
    fn to_buffer_ref<'s>(&'s mut self) -> BufferRef<'d, 's> {
        self.buffer()
    }
}