Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ jobs:
- name: Run tests
run: cargo test

- name: Run tests (no_std)
run: cargo test --no-default-features
- name: Check (no_std)
run: cargo check --no-default-features

- name: Check (no_std, alloc)
run: cargo check --no-default-features --features alloc

- name: Run clippy
run: cargo clippy
Expand Down
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ repository = "https://github.com/tuffy/bitstream-io"
edition = "2018"
rust-version = "1.83"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
core2 = "0.4"
core2 = { version = "0.4", default-features = false }

[features]
std = []
std = ["core2/std", "alloc"]
alloc = ["core2/alloc"]
default = ["std"]
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,12 @@
//! of bit reader or bit writer, regardless of the underlying
//! stream byte source or endianness.

#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![forbid(unsafe_code)]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
Expand All @@ -168,8 +170,11 @@ pub use read::{
BitRead, BitRead2, BitReader, ByteRead, ByteReader, FromBitStream, FromBitStreamUsing,
FromBitStreamWith, FromByteStream, FromByteStreamUsing, FromByteStreamWith,
};
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
pub use write::BitRecorder;
pub use write::{
BitRecorder, BitWrite, BitWrite2, BitWriter, BitsWritten, ByteWrite, ByteWriter, ToBitStream,
BitWrite, BitWrite2, BitWriter, BitsWritten, ByteWrite, ByteWriter, ToBitStream,
ToBitStreamUsing, ToBitStreamWith, ToByteStream, ToByteStreamUsing, ToByteStreamWith,
};

Expand Down Expand Up @@ -2251,10 +2256,7 @@ impl<const BITS: u32, U: UnsignedInteger> private::Checkable for CheckedUnsigned
impl<const BITS: u32, U: UnsignedInteger> CheckablePrimitive for CheckedUnsignedFixed<BITS, U> {
type CountType = FixedBitCount<BITS>;

fn read<R: BitRead + ?Sized>(
reader: &mut R,
count: FixedBitCount<BITS>,
) -> std::io::Result<Self> {
fn read<R: BitRead + ?Sized>(reader: &mut R, count: FixedBitCount<BITS>) -> io::Result<Self> {
Ok(Self {
value: reader.read_unsigned::<BITS, _>()?,
count,
Expand Down Expand Up @@ -2545,7 +2547,7 @@ impl<const BITS: u32, S: SignedInteger> CheckablePrimitive for CheckedSignedFixe
fn read<R: BitRead + ?Sized>(
reader: &mut R,
count: FixedSignedBitCount<BITS>,
) -> std::io::Result<Self> {
) -> io::Result<Self> {
Ok(Self {
value: reader.read_signed::<BITS, _>()?,
count,
Expand Down
13 changes: 12 additions & 1 deletion src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#[cfg(not(feature = "std"))]
use core2::io;

#[cfg(feature = "alloc")]
use alloc::{vec, vec::Vec};
#[cfg(feature = "std")]
use std::io;
Expand Down Expand Up @@ -683,6 +684,8 @@ pub trait BitRead {
/// assert_eq!(r.read_to_vec(3).unwrap().as_slice(), &[0x01, 0x02, 0x03]);
/// assert_eq!(r.read::<8, u8>().unwrap(), 0x04);
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> {
read_to_vec(|buf| self.read_bytes(buf), bytes)
}
Expand Down Expand Up @@ -1011,6 +1014,7 @@ impl<R: BitRead + ?Sized> BitRead for &mut R {
}

#[inline]
#[cfg(feature = "alloc")]
fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> {
(**self).read_to_vec(bytes)
}
Expand Down Expand Up @@ -1209,6 +1213,8 @@ pub trait BitRead2 {
/// # Errors
///
/// Passes along any I/O error from the underlying stream.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> {
read_to_vec(|buf| self.read_bytes(buf), bytes)
}
Expand Down Expand Up @@ -1704,8 +1710,10 @@ where
/// assert_eq!(reader.position_in_bits().unwrap(), 6);
/// ```
#[inline]
#[allow(clippy::seek_from_current)]
pub fn position_in_bits(&mut self) -> io::Result<u64> {
let bytes = self.reader.stream_position()?;
// core2 doesn't have `seek_from_current`
let bytes = self.reader.seek(io::SeekFrom::Current(0))?;
Ok(bytes * 8 - (self.bits as u64))
}
}
Expand Down Expand Up @@ -1818,6 +1826,8 @@ pub trait ByteRead {
/// # Errors
///
/// Passes along any I/O error from the underlying stream.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> {
read_to_vec(|buf| self.read_bytes(buf), bytes)
}
Expand Down Expand Up @@ -2322,6 +2332,7 @@ pub trait FromByteStreamUsing {
Self: Sized;
}

#[cfg(feature = "alloc")]
fn read_to_vec(
mut read: impl FnMut(&mut [u8]) -> io::Result<()>,
bytes: usize,
Expand Down
Loading
Loading