Skip to content

Commit ed9b3cf

Browse files
committed
Pull VBR integer method delegation out into its own VBRInteger trait
This not only maintains semver compatiblity, but it also allows us to exclude a bool implementation altogether.
1 parent 2d82200 commit ed9b3cf

File tree

3 files changed

+87
-85
lines changed

3 files changed

+87
-85
lines changed

src/lib.rs

Lines changed: 82 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,6 @@ pub trait Integer {
294294
R: BitRead + ?Sized,
295295
Self: Sized;
296296

297-
/// Reads a valur of ourself from the stream using a variable width integer.
298-
///
299-
/// # Errors
300-
///
301-
/// Passes along any I/O error from the underlying stream.
302-
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
303-
where
304-
R: BitRead + ?Sized,
305-
Self: Sized;
306-
307297
/// Writes ourself to the stream using the given const number of bits.
308298
///
309299
/// # Errors
@@ -329,6 +319,24 @@ pub trait Integer {
329319
writer: &mut W,
330320
bits: BitCount<MAX>,
331321
) -> io::Result<()>;
322+
}
323+
324+
/// This trait is for integer types which can be read or written
325+
/// to a bit stream as variable-width integers.
326+
///
327+
/// It unifies signed and unsigned integer types by delegating
328+
/// reads and write to the signed and unsigned vbr reading and
329+
/// writing methods as appropriate.
330+
pub trait VBRInteger: Integer {
331+
/// Reads a value of ourself from the stream using a variable width integer.
332+
///
333+
/// # Errors
334+
///
335+
/// Passes along any I/O error from the underlying stream.
336+
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
337+
where
338+
R: BitRead + ?Sized,
339+
Self: Sized;
332340

333341
/// Writes ourself to the stream using a variable width integer.
334342
///
@@ -396,15 +404,6 @@ impl Integer for bool {
396404
}
397405
}
398406

399-
#[inline(always)]
400-
fn read_vbr<const FIELD_SIZE: u32, R>(_reader: &mut R) -> io::Result<Self>
401-
where
402-
R: BitRead + ?Sized,
403-
Self: Sized,
404-
{
405-
unimplemented!("Can't read a VBR boolean")
406-
}
407-
408407
#[inline(always)]
409408
fn write<const BITS: u32, W: BitWrite + ?Sized>(self, writer: &mut W) -> io::Result<()> {
410409
const {
@@ -428,14 +427,6 @@ impl Integer for bool {
428427
))
429428
}
430429
}
431-
432-
#[inline(always)]
433-
fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>(
434-
self,
435-
_writer: &mut W,
436-
) -> io::Result<()> {
437-
unimplemented!("Can't write a VBR for boolean")
438-
}
439430
}
440431

441432
impl<const SIZE: usize, I: Integer + Copy + Default> Integer for [I; SIZE] {
@@ -469,6 +460,23 @@ impl<const SIZE: usize, I: Integer + Copy + Default> Integer for [I; SIZE] {
469460

470461
Ok(a)
471462
}
463+
464+
#[inline]
465+
fn write<const BITS: u32, W: BitWrite + ?Sized>(self, writer: &mut W) -> io::Result<()> {
466+
IntoIterator::into_iter(self).try_for_each(|v| writer.write::<BITS, I>(v))
467+
}
468+
469+
#[inline]
470+
fn write_var<const MAX: u32, W: BitWrite + ?Sized>(
471+
self,
472+
writer: &mut W,
473+
count: BitCount<MAX>,
474+
) -> io::Result<()> {
475+
IntoIterator::into_iter(self).try_for_each(|v| writer.write_counted(count, v))
476+
}
477+
}
478+
479+
impl<const SIZE: usize, I: VBRInteger + Copy + Default> VBRInteger for [I; SIZE] {
472480
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
473481
where
474482
R: BitRead + ?Sized,
@@ -485,20 +493,6 @@ impl<const SIZE: usize, I: Integer + Copy + Default> Integer for [I; SIZE] {
485493
Ok(a)
486494
}
487495

488-
#[inline]
489-
fn write<const BITS: u32, W: BitWrite + ?Sized>(self, writer: &mut W) -> io::Result<()> {
490-
IntoIterator::into_iter(self).try_for_each(|v| writer.write::<BITS, I>(v))
491-
}
492-
493-
#[inline]
494-
fn write_var<const MAX: u32, W: BitWrite + ?Sized>(
495-
self,
496-
writer: &mut W,
497-
count: BitCount<MAX>,
498-
) -> io::Result<()> {
499-
IntoIterator::into_iter(self).try_for_each(|v| writer.write_counted(count, v))
500-
}
501-
502496
fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>(
503497
self,
504498
writer: &mut W,
@@ -701,15 +695,6 @@ macro_rules! define_unsigned_integer {
701695
reader.read_unsigned_counted::<MAX, _>(bits)
702696
}
703697

704-
#[inline(always)]
705-
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
706-
where
707-
R: BitRead + ?Sized,
708-
Self: Sized,
709-
{
710-
reader.read_unsigned_vbr::<FIELD_SIZE, _>()
711-
}
712-
713698
#[inline(always)]
714699
fn write<const BITS: u32, W: BitWrite + ?Sized>(
715700
self,
@@ -726,6 +711,17 @@ macro_rules! define_unsigned_integer {
726711
) -> io::Result<()> {
727712
writer.write_unsigned_counted(bits, self)
728713
}
714+
}
715+
716+
impl VBRInteger for $t {
717+
#[inline(always)]
718+
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
719+
where
720+
R: BitRead + ?Sized,
721+
Self: Sized,
722+
{
723+
reader.read_unsigned_vbr::<FIELD_SIZE, _>()
724+
}
729725

730726
#[inline(always)]
731727
fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>(
@@ -804,16 +800,6 @@ macro_rules! define_unsigned_integer {
804800
}
805801
}
806802

807-
#[inline]
808-
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
809-
where
810-
R: BitRead + ?Sized,
811-
Self: Sized,
812-
{
813-
<$t as Integer>::read_vbr::<FIELD_SIZE, R>(reader)
814-
.map(|u| NonZero::new(u + 1).unwrap())
815-
}
816-
817803
#[inline]
818804
fn write<const BITS: u32, W: BitWrite + ?Sized>(
819805
self,
@@ -844,13 +830,25 @@ macro_rules! define_unsigned_integer {
844830
))
845831
}
846832
}
833+
}
834+
835+
impl VBRInteger for NonZero<$t> {
836+
#[inline]
837+
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
838+
where
839+
R: BitRead + ?Sized,
840+
Self: Sized,
841+
{
842+
<$t as VBRInteger>::read_vbr::<FIELD_SIZE, R>(reader)
843+
.map(|u| NonZero::new(u + 1).unwrap())
844+
}
847845

848846
#[inline]
849847
fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>(
850848
self,
851849
writer: &mut W,
852850
) -> io::Result<()> {
853-
<$t as Integer>::write_vbr::<FIELD_SIZE, W>(self.get() - 1, writer)
851+
<$t as VBRInteger>::write_vbr::<FIELD_SIZE, W>(self.get() - 1, writer)
854852
}
855853
}
856854

@@ -872,15 +870,6 @@ macro_rules! define_unsigned_integer {
872870
<$t as Integer>::read_var::<MAX, R>(reader, count).map(NonZero::new)
873871
}
874872

875-
#[inline(always)]
876-
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
877-
where
878-
R: BitRead + ?Sized,
879-
Self: Sized,
880-
{
881-
<$t as Integer>::read_vbr::<FIELD_SIZE, _>(reader).map(NonZero::new)
882-
}
883-
884873
#[inline]
885874
fn write<const BITS: u32, W: BitWrite + ?Sized>(
886875
self,
@@ -901,13 +890,24 @@ macro_rules! define_unsigned_integer {
901890
count,
902891
)
903892
}
893+
}
894+
895+
impl VBRInteger for Option<NonZero<$t>> {
896+
#[inline(always)]
897+
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
898+
where
899+
R: BitRead + ?Sized,
900+
Self: Sized,
901+
{
902+
<$t as VBRInteger>::read_vbr::<FIELD_SIZE, _>(reader).map(NonZero::new)
903+
}
904904

905905
#[inline]
906906
fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>(
907907
self,
908908
writer: &mut W,
909909
) -> io::Result<()> {
910-
<$t as Integer>::write_vbr::<FIELD_SIZE, W>(
910+
<$t as VBRInteger>::write_vbr::<FIELD_SIZE, W>(
911911
self.map(|n| n.get()).unwrap_or(0),
912912
writer,
913913
)
@@ -1010,15 +1010,6 @@ macro_rules! define_signed_integer {
10101010
reader.read_signed_counted::<MAX, _>(bits)
10111011
}
10121012

1013-
#[inline(always)]
1014-
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
1015-
where
1016-
R: BitRead + ?Sized,
1017-
Self: Sized,
1018-
{
1019-
reader.read_signed_vbr::<FIELD_SIZE, _>()
1020-
}
1021-
10221013
#[inline(always)]
10231014
fn write<const BITS: u32, W: BitWrite + ?Sized>(
10241015
self,
@@ -1035,6 +1026,17 @@ macro_rules! define_signed_integer {
10351026
) -> io::Result<()> {
10361027
writer.write_signed_counted::<MAX, _>(bits, self)
10371028
}
1029+
}
1030+
1031+
impl VBRInteger for $t {
1032+
#[inline(always)]
1033+
fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self>
1034+
where
1035+
R: BitRead + ?Sized,
1036+
Self: Sized,
1037+
{
1038+
reader.read_signed_vbr::<FIELD_SIZE, _>()
1039+
}
10381040

10391041
#[inline(always)]
10401042
fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>(

src/read.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::io;
2020

2121
use super::{
2222
BitCount, CheckablePrimitive, Endianness, Integer, PhantomData, Primitive, SignedBitCount,
23-
SignedInteger, UnsignedInteger,
23+
SignedInteger, UnsignedInteger, VBRInteger,
2424
};
2525

2626
use core::convert::TryInto;
@@ -994,7 +994,7 @@ pub trait BitRead {
994994
}
995995

996996
/// Reads a signed or unsigned variable width integer from the stream.
997-
///
997+
///
998998
/// # Errors
999999
///
10001000
/// Passes along any I/O error from the underlying stream.
@@ -1010,7 +1010,7 @@ pub trait BitRead {
10101010
/// assert_eq!(r.read_vbr::<4, i32>().unwrap(), -50);
10111011
/// ```
10121012
#[inline]
1013-
fn read_vbr<const FIELD_SIZE: u32, I: Integer>(&mut self) -> io::Result<I> {
1013+
fn read_vbr<const FIELD_SIZE: u32, I: VBRInteger>(&mut self) -> io::Result<I> {
10141014
I::read_vbr::<FIELD_SIZE, _>(self)
10151015
}
10161016

src/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::io;
2424

2525
use super::{
2626
BitCount, Checkable, CheckedSigned, CheckedUnsigned, Endianness, Integer, Numeric, PhantomData,
27-
Primitive, SignedBitCount, SignedInteger, UnsignedInteger,
27+
Primitive, SignedBitCount, SignedInteger, UnsignedInteger, VBRInteger,
2828
};
2929

3030
#[cfg(feature = "alloc")]
@@ -1050,7 +1050,7 @@ pub trait BitWrite {
10501050
/// assert_eq!(writer.into_writer(), [0b0110_1011, 0b1100_0001]);
10511051
/// ```
10521052
#[inline]
1053-
fn write_vbr<const FIELD_SIZE: u32, I: Integer>(&mut self, value: I) -> io::Result<()> {
1053+
fn write_vbr<const FIELD_SIZE: u32, I: VBRInteger>(&mut self, value: I) -> io::Result<()> {
10541054
I::write_vbr::<FIELD_SIZE, _>(value, self)
10551055
}
10561056

0 commit comments

Comments
 (0)