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
//-
// Copyright 2017 Jason Lingle
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Specialised adapters for doing IO.

use std::io::{self, BufRead, Read, Seek, Write};

/// A simple wrapper around `io::Cursor` which exposes `AsRef<[u8]>` and
/// `AsMut<[u8]>` for use with `Blob::slice()` and `Blob::slice_mut()`.
///
/// Note that the `AsRef` and `AsMut` implementations return the *unconsumed*
/// portion of the slice rather than the whole thing.
#[derive(Debug, Clone)]
pub struct TransparentCursor<T>(#[allow(missing_docs)] pub io::Cursor<T>);
impl<T : AsRef<[u8]>> Read for TransparentCursor<T> {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.read(buf)
    }
}

impl<T : AsRef<[u8]>> BufRead for TransparentCursor<T> {
    #[inline]
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        self.0.fill_buf()
    }
    #[inline]
    fn consume(&mut self, amt: usize) {
        self.0.consume(amt)
    }
}

impl<T> Write for TransparentCursor<T> where io::Cursor<T> : Write {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.0.write(buf)
    }
    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        self.0.flush()
    }
}

impl<T : AsRef<[u8]>> Seek for TransparentCursor<T> {
    fn seek(&mut self, whence: io::SeekFrom) -> io::Result<u64> {
        self.0.seek(whence)
    }
}

impl<T : AsRef<[u8]>> AsRef<[u8]> for TransparentCursor<T> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        let pos = self.0.position() as usize;
        &self.0.get_ref().as_ref()[pos..]
    }
}

impl<T : AsMut<[u8]>> AsMut<[u8]> for TransparentCursor<T> {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        let pos = self.0.position() as usize;
        &mut self.0.get_mut().as_mut()[pos..]
    }
}

/// Like `AsRef<[T]>`, but the lifetime of the slice is independent of `self`.
pub trait AsExtBytes<'a> {
    /// Returns a reference the bytes of this buffer.
    fn as_ext_bytes(&mut self) -> &'a [u8];
}

impl<'a> AsExtBytes<'a> for &'a [u8] {
    fn as_ext_bytes(&mut self) -> &'a [u8] {
        self
    }
}

impl<'a> AsExtBytes<'a> for io::Cursor<&'a [u8]> {
    fn as_ext_bytes(&mut self) -> &'a [u8] {
        let buf: &'a [u8] = *self.get_ref();
        &buf[(self.position() as usize)..]
    }
}

impl<'a> AsExtBytes<'a> for TransparentCursor<&'a [u8]> {
    fn as_ext_bytes(&mut self) -> &'a [u8] {
        self.0.as_ext_bytes()
    }
}