Skip to main content

libtw2_buffer/impls/
buffer_ref.rs

1use crate::wildly_unsafe;
2use crate::Buffer;
3use crate::BufferRef;
4use crate::ToBufferRef;
5
6/// The intermediate step from a `BufferRef` to another `BufferRef`.
7pub struct BufferRefBuffer<'ref_, 'data: 'ref_, 'size: 'ref_> {
8    // Will only touch the length of the `BufferRef` through this reference,
9    // except in `BufferRefBuffer::buffer`.
10    buffer: &'ref_ mut BufferRef<'data, 'size>,
11    initialized: usize,
12}
13
14impl<'r, 'd, 's> BufferRefBuffer<'r, 'd, 's> {
15    fn new(buffer: &'r mut BufferRef<'d, 's>) -> BufferRefBuffer<'r, 'd, 's> {
16        BufferRefBuffer {
17            buffer: buffer,
18            initialized: 0,
19        }
20    }
21    fn buffer<'a>(&'a mut self) -> BufferRef<'d, 'a> {
22        let len = *self.buffer.initialized_;
23        unsafe {
24            BufferRef::new(
25                wildly_unsafe(&mut self.buffer.buffer[len..]),
26                &mut self.initialized,
27            )
28        }
29    }
30}
31
32impl<'r, 'd, 's> Drop for BufferRefBuffer<'r, 'd, 's> {
33    fn drop(&mut self) {
34        *self.buffer.initialized_ += self.initialized;
35    }
36}
37
38impl<'r, 'd, 's> Buffer<'d> for &'r mut BufferRef<'d, 's> {
39    type Intermediate = BufferRefBuffer<'r, 'd, 's>;
40    fn to_to_buffer_ref(self) -> Self::Intermediate {
41        BufferRefBuffer::new(self)
42    }
43}
44
45impl<'r, 'd, 's> ToBufferRef<'d> for BufferRefBuffer<'r, 'd, 's> {
46    fn to_buffer_ref<'a>(&'a mut self) -> BufferRef<'d, 'a> {
47        self.buffer()
48    }
49}
50
51#[cfg(test)]
52mod test {
53    use crate::with_buffer;
54
55    #[test]
56    fn buffer_ref_remaining() {
57        let a: &mut [u8] = &mut [0; 32];
58        with_buffer(a, |mut b| {
59            with_buffer(&mut b, |mut c| {
60                assert!(c.remaining() == 32);
61                c.write(&[0]).unwrap();
62                assert!(c.remaining() == 31);
63                assert!(c.initialized() == &[0]);
64            });
65            assert!(b.remaining() == 31);
66            with_buffer(&mut b, |mut c| {
67                assert!(c.remaining() == 31);
68                c.write(&[1]).unwrap();
69                assert!(c.remaining() == 30);
70                assert!(c.initialized() == &[1]);
71            });
72            assert!(b.remaining() == 30);
73            assert!(b.initialized() == &[0, 1]);
74        })
75    }
76}