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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#![cfg_attr(feature = "nightly",
    feature(external_doc),
    doc(include = "../README.md"),
)]

#[macro_use]
extern crate require_unsafe_in_body;

#[macro_use]
mod utils;
#[path = "utils/prelude.rs"]
mod prelude;

use_prelude!();

mod impls;

pub use self::traits::{
    MaybeUninitExt,
    slice::{
        InitWithCopyFromSlice,
    },
    vec::{
        VecExtendFromReader,
        VecReserveUninit,
    },
};
mod traits;

#[doc(hidden)]
pub use ::core;

/// Sets up an inline / stack-allocated array of
/// [uninitialized bytes][`MaybeUninit`].
///
/// # Example
///
/// ```rust
/// use ::uninit::{ReadIntoUninit, uninit_byte_array};
///
/// let mut reader = &b"Hello, World!"[..];
/// let mut array = uninit_byte_array![_; 4]; // : [MaybeUninit<u8>; 4]
/// assert_eq!(
///     reader.read_into_uninit_exact(&mut array).unwrap(),
///     b"Hell",
/// );
/// ```
#[macro_export]
macro_rules! uninit_byte_array {(
    _; $count:expr
) => ({
    use $crate::core::mem::MaybeUninit;
    type Array = [MaybeUninit<u8>; $count];
    unsafe {
        // # Safety
        //
        //   - mem::uninitialized::<[MaybeUninit<_>; _]>() is sound.
        MaybeUninit::<Array>::uninit().assume_init()
    }
})}

/// Basically [`AsMut`]`<[`[`MaybeUninit`]`<u8>]>` but with a more explicit
/// name.
pub
trait AsUninitSliceMut {
    fn as_uninit_slice_mut (self: &'_ mut Self)
        -> &'_ mut [MaybeUninit<u8>]
    ;
}

/// Init bytes can be seen as uninit.
impl AsUninitSliceMut for [u8]
{
    #[inline]
    fn as_uninit_slice_mut (self: &'_ mut Self)
        -> &'_ mut [MaybeUninit<u8>]
    {
        <[MaybeUninit<u8>]>::from_mut(self)
    }
}

/// Init bytes can be seen as uninit.
impl AsUninitSliceMut for Vec<u8>
{
    #[inline]
    fn as_uninit_slice_mut (self: &'_ mut Self)
        -> &'_ mut [MaybeUninit<u8>]
    {
        <[MaybeUninit<u8>]>::from_mut(self)
    }
}

/// Identity
impl AsUninitSliceMut for [MaybeUninit<u8>] {
    /// This can be trusted to return the same slice as given in input,
    /// so that initializing the returned slice is guaranteed to have initialized
    /// the input slice.
    #[inline]
    fn as_uninit_slice_mut<'__> (self: &'__ mut [MaybeUninit<u8>])
        -> &'__ mut [MaybeUninit<u8>]
    {
        self
    }
}

#[cfg(not(feature = "downcast_as_ReadIntoUninit"))]
use io::Read;

#[cfg(feature = "downcast_as_ReadIntoUninit")]
// With specialization this can be overriden
impl<R : ?Sized + io::Read> Read for R {
    #[inline]
    // default
    fn downcast_read_uninit (self: &'_ mut Self)
        -> Option<&'_ mut dyn ReadIntoUninit>
    {
        None
    }
}

#[cfg(feature = "downcast_as_ReadIntoUninit")]
pub
trait Read: io::Read {
    #[allow(bad_style)]
    #[inline]
    fn downcast_as_ReadIntoUninit (self: &'_ mut Self)
        -> Option<&'_ mut dyn ReadIntoUninit>
    {
        None
    }

    fn mb_read_into_uninit<'buf> (
        self: &'_ mut Self,
        buf: &'buf mut [MaybeUninit<u8>],
    )   -> io::Result<&'buf mut [u8]>
    {
        if let Some(uninit_reader) = self.downcast_as_ReadIntoUninit() {
            uninit_reader
                .read_into_uninit(buf)
        } else {
            buf.iter_mut().for_each(|at_uninit_byte| {
                *at_uninit_byte = MaybeUninit::new(0);
            });
            let buf: &mut [u8] = unsafe {
                // # Safety
                //
                //   - buf has been initialized
                <[MaybeUninit<u8>]>::assume_init_by_mut(buf)
            };
            let n = self.read(buf)?;
            Ok(&mut buf[.. n])
        }
    }
}

/// Trait for a [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html)able
/// type that can output the bytes read into
/// [uninitialised memory][`MaybeUninit`].
///
/// # Safety
///
/// The trait is marked `unsafe` because it **needs to guarantee** that:
///
///   - `if let Ok(init_buf) = self.read_into_uninit(buf)`, then
///     `init_buf` is a prefix slice of `buf`.
///
///       - this property is equivalent to:
///
///         `init_buf.as_mut_ptr() == buf.as_mut_ptr() as *mut u8` and
///         `init_buf.len() <= buf.len()`
///
///       - as well as:
///
///         **`buf[.. init_buf.len()]` is sound to `assume_init`**
///
/// `unsafe` code can assume this property to skip checks or manual
/// initialization, and that's why incorrectly `impl`-ementing this marker
/// trait can compromise memory safety.
pub
unsafe
trait ReadIntoUninit : Read
{
    /// "Read into uninitialized memory" logic.
    #[inline]
    fn read_into_uninit<'buf> (
        self: &'_ mut Self,
        buf: &'buf mut [MaybeUninit<u8>],
    )   -> io::Result<&'buf mut [u8]>
    ;

    /// "Read into uninitialized memory" logic.
    // # Safety
    //   - delegates to `self.read_into_uninit()` calls.
    #[inline]
    fn read_into_uninit_exact<'buf> (
        self: &'_ mut Self,
        buf: &'buf mut [MaybeUninit<u8>],
    )   -> io::Result<&'buf mut [u8]>
    {
        {
            let mut buf = &mut *buf;
            while buf.is_empty().not() {
                match self.read_into_uninit(buf).map(|x| x.len()) {
                    | Ok(0) => break,
                    | Ok(n) => buf = &mut buf[n ..],
                    | Err(ref e)
                        if e.kind() == io::ErrorKind::Interrupted
                    => {},
                    | Err(e) => return Err(e),
                }
            }
            if buf.is_empty().not() {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "failed to fill whole buffer",
                ));
            }
        }
        Ok(unsafe {
            // # Safety
            //
            //   - this is the "concatenation" of all the "buf[.. n]"
            //     initialisation witnesses.
            <[MaybeUninit<u8>]>::assume_init_by_mut(buf)
        })
    }

    #[cfg(feature = "chain")]
    fn chain <R : ReadIntoUninit> (
        self: Self,
        next: R,
    )   -> Chain<Self, R>
    where
        Self : Sized,
    {
        Chain { first: self, second: next, first_done: false }
    }

}

#[allow(unused_macros)]
macro_rules! auto_impl {(
    #[derived_from(ReadIntoUninit)]
    impl [$($generics:tt)*] io::Read for $T:ty
    where
        $($where_clause:tt)*
) => (
    impl<$($generics)*> io::Read for $T
    where
        $($where_clause)*
    {
        #[inline]
        fn read (
            self: &'_ mut Self,
            buf: &'_ mut [u8],
        )   -> io::Result<usize>
        {
            <Self as ReadIntoUninit>::read_into_uninit(
                self,
                buf.as_uninit_slice_mut(),
            ).map(|x| x.len())
        }
    }
)}

#[cfg(feature = "chain")]
mod chain {
    use super::*;

    #[derive(Debug)]
    pub
    struct Chain<R1, R2>
    where
        R1 : ReadIntoUninit,
        R2 : ReadIntoUninit,
    {
        first: R1,
        second: R2,
        first_done: bool,
    }

    impl<R1, R2> Chain<R1, R2>
    where
        R1 : ReadIntoUninit,
        R2 : ReadIntoUninit,
    {
        pub
        fn into_inner (self: Self)
            -> (R1, R2)
        {
            let Self { first, second, ..} = self;
            (first, second)
        }

        pub
        fn get_ref (self: &'_ Self)
            -> (&'_ R1, &'_ R2)
        {
            let Self { first, second, ..} = self;
            (first, second)
        }
    }

    /// This test verifies the soundness of the lifetime erasure in
    /// `<Chain<_, _> as ReadIntoUninit>::read_into_uninit()`
    #[cfg(all(
        test,
        not(feature = "polonius-check"),
    ))]
    #[test]
    fn check ()
    {
        assert!(
            ::std::process::Command::new("cargo")
                .arg("+nightly")
                .arg("rustc")
                .arg("--features")
                .arg(
                    [
                        "chain",
                        "polonius-check",
                    ].join(" ")
                )
                .arg("--")
                .arg("-Zpolonius")
                .status()
                .unwrap()
                .success()
        );
    }

    unsafe
    impl<R1, R2> ReadIntoUninit for Chain<R1, R2>
    where
        R1 : ReadIntoUninit,
        R2 : ReadIntoUninit,
    {
        fn read_into_uninit<'buf> (
            self: &'_ mut Self,
            buf: &'buf mut [MaybeUninit<u8>],
        )   -> io::Result<&'buf mut [u8]>
        {
            if buf.len() == 0 {
                return Ok(unsafe {
                    // # Safety
                    //
                    //   - since it has `0` elements,
                    //     it does have `0` initialized elements.
                    <[MaybeUninit<u8>]>::assume_init_by_mut(buf)
                })
            }
            if self.first_done.not() {
                #[cfg(not(feature = "polonius-check"))]
                let buf: &mut [MaybeUninit<u8>] = unsafe {
                    // Lifetime hack while waiting for polonius
                    //
                    // # Safety
                    //
                    //   - `cargo rustc --features polonius-check -- -Zpolonius`
                    //     passes.
                    slice::from_raw_parts_mut(
                        buf.as_mut_ptr(),
                        buf.len(),
                    )
                };
                let buf = self.first.read_into_uninit(buf)?;
                if buf.is_empty() {
                    self.first_done = true;
                } else {
                    return Ok(buf);
                }
            }
            self.second.read_into_uninit(buf)
        }
    }

    auto_impl!(
        #[derived_from(ReadIntoUninit)]
        impl[R1, R2] io::Read for Chain<R1, R2>
        where
            R1 : ReadIntoUninit,
            R2 : ReadIntoUninit,
    );
}