bitcoin_io/lib.rs
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
//! Rust-Bitcoin IO Library
//!
//! The `std::io` module is not exposed in `no-std` Rust so building `no-std` applications which
//! require reading and writing objects via standard traits is not generally possible. Thus, this
//! library exists to export a minmal version of `std::io`'s traits which we use in `rust-bitcoin`
//! so that we can support `no-std` applications.
//!
//! These traits are not one-for-one drop-ins, but are as close as possible while still implementing
//! `std::io`'s traits without unnecessary complexity.
#![cfg_attr(not(feature = "std"), no_std)]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
// Coding conventions.
#![warn(missing_docs)]
// Exclude lints we don't think are valuable.
#![allow(clippy::needless_question_mark)] // https://github.com/rust-bitcoin/rust-bitcoin/pull/2134
#![allow(clippy::manual_range_contains)] // More readable than clippy's format.
#[cfg(feature = "alloc")]
extern crate alloc;
mod error;
mod macros;
#[cfg(feature = "std")]
mod bridge;
#[cfg(feature = "std")]
pub use bridge::{FromStd, ToStd};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::vec::Vec;
use core::cmp;
#[rustfmt::skip] // Keep public re-exports separate.
pub use self::error::{Error, ErrorKind};
/// Result type returned by functions in this crate.
pub type Result<T> = core::result::Result<T, Error>;
/// A generic trait describing an input stream. See [`std::io::Read`] for more info.
pub trait Read {
/// Reads bytes from source into `buf`.
fn read(&mut self, buf: &mut [u8]) -> Result<usize>;
/// Reads bytes from source until `buf` is full.
#[inline]
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> {
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => return Err(ErrorKind::UnexpectedEof.into()),
Ok(len) => buf = &mut buf[len..],
Err(e) if e.kind() == ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(())
}
/// Creates an adapter which will read at most `limit` bytes.
#[inline]
fn take(&mut self, limit: u64) -> Take<Self> { Take { reader: self, remaining: limit } }
/// Attempts to read up to limit bytes from the reader, allocating space in `buf` as needed.
///
/// `limit` is used to prevent a denial of service attack vector since an unbounded reader will
/// exhaust all memory.
///
/// Similar to `std::io::Read::read_to_end` but with the DOS protection.
#[doc(alias = "read_to_end")]
#[cfg(feature = "alloc")]
#[inline]
fn read_to_limit(&mut self, buf: &mut Vec<u8>, limit: u64) -> Result<usize> {
self.take(limit).read_to_end(buf)
}
}
/// A trait describing an input stream that uses an internal buffer when reading.
pub trait BufRead: Read {
/// Returns data read from this reader, filling the internal buffer if needed.
fn fill_buf(&mut self) -> Result<&[u8]>;
/// Marks the buffered data up to amount as consumed.
///
/// # Panics
///
/// May panic if `amount` is greater than amount of data read by `fill_buf`.
fn consume(&mut self, amount: usize);
}
/// Reader adapter which limits the bytes read from an underlying reader.
///
/// Created by calling `[Read::take]`.
pub struct Take<'a, R: Read + ?Sized> {
reader: &'a mut R,
remaining: u64,
}
impl<'a, R: Read + ?Sized> Take<'a, R> {
/// Reads all bytes until EOF from the underlying reader into `buf`.
#[cfg(feature = "alloc")]
#[inline]
pub fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
let mut read: usize = 0;
let mut chunk = [0u8; 64];
loop {
match self.read(&mut chunk) {
Ok(0) => break,
Ok(n) => {
buf.extend_from_slice(&chunk[0..n]);
read += n;
}
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
Err(e) => return Err(e),
};
}
Ok(read)
}
}
impl<'a, R: Read + ?Sized> Read for Take<'a, R> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let len = cmp::min(buf.len(), self.remaining.try_into().unwrap_or(buf.len()));
let read = self.reader.read(&mut buf[..len])?;
self.remaining -= read.try_into().unwrap_or(self.remaining);
Ok(read)
}
}
// Impl copied from Rust stdlib.
impl<'a, R: BufRead + ?Sized> BufRead for Take<'a, R> {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8]> {
// Don't call into inner reader at all at EOF because it may still block
if self.remaining == 0 {
return Ok(&[]);
}
let buf = self.reader.fill_buf()?;
// Cast length to a u64 instead of casting `remaining` to a `usize`
// (in case `remaining > u32::MAX` and we are on a 32 bit machine).
let cap = cmp::min(buf.len() as u64, self.remaining) as usize;
Ok(&buf[..cap])
}
#[inline]
fn consume(&mut self, amount: usize) {
assert!(amount as u64 <= self.remaining);
self.remaining -= amount as u64;
self.reader.consume(amount);
}
}
impl Read for &[u8] {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let cnt = cmp::min(self.len(), buf.len());
buf[..cnt].copy_from_slice(&self[..cnt]);
*self = &self[cnt..];
Ok(cnt)
}
}
impl BufRead for &[u8] {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8]> { Ok(self) }
// This panics if amount is out of bounds, same as the std version.
#[inline]
fn consume(&mut self, amount: usize) { *self = &self[amount..] }
}
/// Wraps an in memory reader providing the `position` function.
pub struct Cursor<T> {
inner: T,
pos: u64,
}
impl<T: AsRef<[u8]>> Cursor<T> {
/// Creates a `Cursor` by wrapping `inner`.
#[inline]
pub fn new(inner: T) -> Self { Cursor { inner, pos: 0 } }
/// Returns the position read up to thus far.
#[inline]
pub fn position(&self) -> u64 { self.pos }
/// Sets the internal position.
///
/// This method allows seeking within the wrapped memory by setting the position.
///
/// Note that setting a position that is larger than the buffer length will cause reads to
/// return no bytes (EOF).
#[inline]
pub fn set_position(&mut self, position: u64) {
self.pos = position;
}
/// Returns the inner buffer.
///
/// This is the whole wrapped buffer, including the bytes already read.
#[inline]
pub fn into_inner(self) -> T { self.inner }
/// Returns a reference to the inner buffer.
///
/// This is the whole wrapped buffer, including the bytes already read.
#[inline]
pub fn inner(&self) -> &T { &self.inner }
}
impl<T: AsRef<[u8]>> Read for Cursor<T> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let inner: &[u8] = self.inner.as_ref();
let start_pos = self.pos.try_into().unwrap_or(inner.len());
let read = core::cmp::min(inner.len().saturating_sub(start_pos), buf.len());
buf[..read].copy_from_slice(&inner[start_pos..start_pos + read]);
self.pos =
self.pos.saturating_add(read.try_into().unwrap_or(u64::MAX /* unreachable */));
Ok(read)
}
}
impl<T: AsRef<[u8]>> BufRead for Cursor<T> {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8]> {
let inner: &[u8] = self.inner.as_ref();
Ok(&inner[self.pos as usize..])
}
#[inline]
fn consume(&mut self, amount: usize) {
assert!(amount <= self.inner.as_ref().len());
self.pos += amount as u64;
}
}
/// A generic trait describing an output stream. See [`std::io::Write`] for more info.
pub trait Write {
/// Writes `buf` into this writer, returning how many bytes were written.
fn write(&mut self, buf: &[u8]) -> Result<usize>;
/// Flushes this output stream, ensuring that all intermediately buffered contents
/// reach their destination.
fn flush(&mut self) -> Result<()>;
/// Attempts to write an entire buffer into this writer.
#[inline]
fn write_all(&mut self, mut buf: &[u8]) -> Result<()> {
while !buf.is_empty() {
match self.write(buf) {
Ok(0) => return Err(ErrorKind::UnexpectedEof.into()),
Ok(len) => buf = &buf[len..],
Err(e) if e.kind() == ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(())
}
}
#[cfg(feature = "alloc")]
impl Write for alloc::vec::Vec<u8> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.extend_from_slice(buf);
Ok(buf.len())
}
#[inline]
fn flush(&mut self) -> Result<()> { Ok(()) }
}
impl<'a> Write for &'a mut [u8] {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let cnt = core::cmp::min(self.len(), buf.len());
self[..cnt].copy_from_slice(&buf[..cnt]);
*self = &mut core::mem::take(self)[cnt..];
Ok(cnt)
}
#[inline]
fn flush(&mut self) -> Result<()> { Ok(()) }
}
/// A sink to which all writes succeed. See [`std::io::Sink`] for more info.
///
/// Created using `io::sink()`.
pub struct Sink;
impl Write for Sink {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize> { Ok(buf.len()) }
#[inline]
fn write_all(&mut self, _: &[u8]) -> Result<()> { Ok(()) }
#[inline]
fn flush(&mut self) -> Result<()> { Ok(()) }
}
/// Returns a sink to which all writes succeed. See [`std::io::sink`] for more info.
#[inline]
pub fn sink() -> Sink { Sink }
/// Wraps a `std` IO type to implement the traits from this crate.
///
/// All methods are passed through converting the errors.
#[cfg(feature = "std")]
#[inline]
pub const fn from_std<T>(std_io: T) -> FromStd<T> {
FromStd::new(std_io)
}
/// Wraps a mutable reference to `std` IO type to implement the traits from this crate.
///
/// All methods are passed through converting the errors.
#[cfg(feature = "std")]
#[inline]
pub fn from_std_mut<T>(std_io: &mut T) -> &mut FromStd<T> {
FromStd::new_mut(std_io)
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::{string::ToString, vec};
#[test]
fn buf_read_fill_and_consume_slice() {
let data = [0_u8, 1, 2];
let mut slice = &data[..];
let fill = BufRead::fill_buf(&mut slice).unwrap();
assert_eq!(fill.len(), 3);
assert_eq!(fill, &[0_u8, 1, 2]);
slice.consume(2);
let fill = BufRead::fill_buf(&mut slice).unwrap();
assert_eq!(fill.len(), 1);
assert_eq!(fill, &[2_u8]);
slice.consume(1);
// checks we can attempt to read from a now-empty reader.
let fill = BufRead::fill_buf(&mut slice).unwrap();
assert_eq!(fill.len(), 0);
assert_eq!(fill, &[]);
}
#[test]
#[cfg(feature = "alloc")]
fn read_to_limit_greater_than_total_length() {
let s = "16-byte-string!!".to_string();
let mut reader = Cursor::new(&s);
let mut buf = vec![];
// 32 is greater than the reader length.
let read = reader.read_to_limit(&mut buf, 32).expect("failed to read to limit");
assert_eq!(read, s.len());
assert_eq!(&buf, s.as_bytes())
}
#[test]
#[cfg(feature = "alloc")]
fn read_to_limit_less_than_total_length() {
let s = "16-byte-string!!".to_string();
let mut reader = Cursor::new(&s);
let mut buf = vec![];
let read = reader.read_to_limit(&mut buf, 2).expect("failed to read to limit");
assert_eq!(read, 2);
assert_eq!(&buf, "16".as_bytes())
}
}