| 1 | // Copyright (c) 2015-2023 Georg Brandl. Licensed under the Apache License, |
| 2 | // Version 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> |
| 3 | // or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at |
| 4 | // your option. This file may not be copied, modified, or distributed except |
| 5 | // according to those terms. |
| 6 | |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 8 | |
| 9 | use craterust-analyzer cargo iter-read 1.1.0 crate/::IterReadItemrust-analyzer cargo iter-read 1.1.0 IterReadItem#; |
| 10 | |
| 11 | |
| 12 | macro_rules! impl_byterust-analyzer cargo iter-read 1.1.0 impls/impl_byte! { |
| 13 | ($el_ty:ty, $($deref:tt)*) => { |
| 14 | impl<'a> IterReadItem for $el_ty { |
| 15 | impl_byte!(@inner [$($deref)*] []); |
| 16 | } |
| 17 | |
| 18 | impl<'a, E> IterReadItem for Result<$el_ty, E> where io::Error: From<E> { |
| 19 | impl_byte!(@inner [$($deref)*] [?]); |
| 20 | } |
| 21 | }; |
| 22 | (@inner [$($deref:tt)*] [$($try:tt)*]) => { |
| 23 | type Buffer = (); |
| 24 | |
| 25 | fn read<I: Iterator<Item=Self>>(target: &mut [u8], it: &mut I, |
| 26 | _: &mut ()) -> io::Result<usize> { |
| 27 | let mut len = 0; |
| 28 | for (slot, elt) in target.iter_mut().zip(it) { |
| 29 | *slot = $($deref)* elt $($try)*; |
| 30 | len += 1; |
| 31 | } |
| 32 | Ok(len) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub struct Bufrust-analyzer cargo iter-read 1.1.0 impls/Buf#<T> { |
| 38 | bytesrust-analyzer cargo iter-read 1.1.0 impls/Buf#bytes.: T, |
| 39 | consumedrust-analyzer cargo iter-read 1.1.0 impls/Buf#consumed.: usize, |
| 40 | } |
| 41 | |
| 42 | macro_rules! impl_buf_defaultrust-analyzer cargo iter-read 1.1.0 impls/impl_buf_default! { |
| 43 | ($ty:ty) => { |
| 44 | impl<'a> Default for Buf<$ty> { |
| 45 | fn default() -> Self { |
| 46 | Buf { bytes: Default::default(), consumed: 0 } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl_buf_defaultrust-analyzer cargo iter-read 1.1.0 impls/impl_buf_default!!(&'a [u8]); |
| 53 | impl_buf_defaultrust-analyzer cargo iter-read 1.1.0 impls/impl_buf_default!!(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>); |
| 54 | |
| 55 | impl<const N: usize> Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Bufrust-analyzer cargo iter-read 1.1.0 impls/Buf#<[u8; N]> { |
| 56 | fn defaultrust-analyzer cargo iter-read 1.1.0 impls/impl#[`Buf<[u8; N]>`][Default]default().() -> Selfrust-analyzer cargo iter-read 1.1.0 impls/impl#[`Buf<[u8; N]>`][Default] { |
| 57 | // The default needs to look like "everything is consumed", |
| 58 | // and the array has length N, so consumed needs to be N too. |
| 59 | Bufrust-analyzer cargo iter-read 1.1.0 impls/Buf# { bytesrust-analyzer cargo iter-read 1.1.0 impls/Buf#bytes.: [0; N], consumedrust-analyzer cargo iter-read 1.1.0 impls/Buf#consumed.: N } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | macro_rules! impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like! { |
| 64 | ($el_ty:ty [$($const:tt)*], $buf_ty:ty, $conv:ident) => { |
| 65 | impl<'a, $($const)*> IterReadItem for $el_ty { |
| 66 | impl_slice_like!(@inner $buf_ty, $conv, buf: |
| 67 | Some(v) => { |
| 68 | *buf = Buf { bytes: v.$conv(), consumed: 0 }; |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | impl<'a, E, $($const)*> IterReadItem for Result<$el_ty, E> where io::Error: From<E> { |
| 73 | impl_slice_like!(@inner $buf_ty, $conv, buf: |
| 74 | Some(Ok(v)) => { |
| 75 | *buf = Buf { bytes: v.$conv(), consumed: 0 }; |
| 76 | } |
| 77 | Some(Err(err)) => { |
| 78 | *buf = Buf::default(); |
| 79 | return Err(err.into()); |
| 80 | }); |
| 81 | } |
| 82 | }; |
| 83 | (@inner $buf_ty:ty, $conv:ident, $buf:ident : $($match:tt)+) => { |
| 84 | type Buffer = Buf<$buf_ty>; |
| 85 | |
| 86 | fn read<I: Iterator<Item=Self>>(target: &mut [u8], it: &mut I, |
| 87 | $buf: &mut Self::Buffer) -> io::Result<usize> { |
| 88 | while $buf.consumed == $buf.bytes.len() { |
| 89 | match it.next() { |
| 90 | $($match)+ |
| 91 | None => { |
| 92 | *$buf = Buf::default(); |
| 93 | return Ok(0); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | let mut len = 0; |
| 98 | for (slot, elt) in target.iter_mut().zip(&$buf.bytes[$buf.consumed..]) { |
| 99 | *slot = *elt; |
| 100 | len += 1; |
| 101 | } |
| 102 | $buf.consumed += len; |
| 103 | Ok(len) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | impl_byterust-analyzer cargo iter-read 1.1.0 impls/impl_byte!!(u8, ); |
| 109 | impl_byterust-analyzer cargo iter-read 1.1.0 impls/impl_byte!!(&'a u8, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().); |
| 110 | |
| 111 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!(&'a [u8] [], &'a [u8], intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().); |
| 112 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!(&'a Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> [], &'a [u8], as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().); |
| 113 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> [], Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().); |
| 114 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!([u8; N] [const N: usize], [u8; N], intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().); |
| 115 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!(&'a [u8; N] [const N: usize], &'a [u8], as_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_slice().); |
| 116 | |
| 117 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!(&'a str [], &'a [u8], as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().); |
| 118 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!(&'a Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# [], &'a [u8], as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().); |
| 119 | impl_slice_likerust-analyzer cargo iter-read 1.1.0 impls/impl_slice_like!!(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# [], Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().); |