Merge rust-bitcoin/rust-bitcoin#2264: Improve the `io` crate

b58c235733 io: Make crate MSRV 1.56.1 (Tobin C. Harding)
063dac03bd Import core::cmp (Tobin C. Harding)
c038d00bd1 io: Move use statement (Tobin C. Harding)
21c44df76e inline sink function (Tobin C. Harding)
5610b9a6b6 Remove unnecessary rustdoc (Tobin C. Harding)

Pull request description:

  In preparation for adding the `BufRead` trait do a few improvements, the first ones are all trivial, the last sets the MSRV to Rust 1.56.1

ACKs for top commit:
  Kixunil:
    ACK b58c235733
  apoelstra:
    ACK b58c235733

Tree-SHA512: e750cd615d9423f4a581b80acb8c928d537538703e2079d7bb700e6155eee4bd9b768c1eb53b0b2aa841497474460ec5926ac59c11eea1722f028efacc87c813
This commit is contained in:
Andrew Poelstra 2023-12-13 18:22:14 +00:00
commit 8afa379f39
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 8 additions and 7 deletions

View File

@ -9,7 +9,8 @@ description = "Simple I/O traits for no-std (and std) environments"
categories = ["no-std"] categories = ["no-std"]
keywords = [ "io", "no-std" ] keywords = [ "io", "no-std" ]
readme = "README.md" readme = "README.md"
edition = "2018" edition = "2021"
rust-version = "1.56.1"
exclude = ["tests", "contrib"] exclude = ["tests", "contrib"]
[features] [features]

View File

@ -23,13 +23,12 @@ extern crate alloc;
mod error; mod error;
mod macros; mod macros;
use core::convert::TryInto;
use core::cmp;
#[rustfmt::skip] // Keep public re-exports separate. #[rustfmt::skip] // Keep public re-exports separate.
pub use self::error::{Error, ErrorKind}; pub use self::error::{Error, ErrorKind};
/// Standard I/O stream definitions which are API-equivalent to `std`'s `io` module. See
/// [`std::io`] for more info.
use core::convert::TryInto;
pub type Result<T> = core::result::Result<T, Error>; pub type Result<T> = core::result::Result<T, Error>;
/// A generic trait describing an input stream. See [`std::io::Read`] for more info. /// A generic trait describing an input stream. See [`std::io::Read`] for more info.
@ -61,7 +60,7 @@ pub struct Take<'a, R: Read + ?Sized> {
impl<'a, R: Read + ?Sized> Read for Take<'a, R> { impl<'a, R: Read + ?Sized> Read for Take<'a, R> {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let len = core::cmp::min(buf.len(), self.remaining.try_into().unwrap_or(buf.len())); let len = cmp::min(buf.len(), self.remaining.try_into().unwrap_or(buf.len()));
let read = self.reader.read(&mut buf[..len])?; let read = self.reader.read(&mut buf[..len])?;
self.remaining -= read.try_into().unwrap_or(self.remaining); self.remaining -= read.try_into().unwrap_or(self.remaining);
Ok(read) Ok(read)
@ -80,7 +79,7 @@ impl<R: std::io::Read> Read for R {
impl Read for &[u8] { impl Read for &[u8] {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> { fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let cnt = core::cmp::min(self.len(), buf.len()); let cnt = cmp::min(self.len(), buf.len());
buf[..cnt].copy_from_slice(&self[..cnt]); buf[..cnt].copy_from_slice(&self[..cnt]);
*self = &self[cnt..]; *self = &self[cnt..];
Ok(cnt) Ok(cnt)
@ -201,4 +200,5 @@ impl std::io::Write for Sink {
} }
/// Returns a sink to which all writes succeed. See [`std::io::sink`] for more info. /// Returns a sink to which all writes succeed. See [`std::io::sink`] for more info.
#[inline]
pub fn sink() -> Sink { Sink } pub fn sink() -> Sink { Sink }