@@ -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
441432impl < 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 >(
0 commit comments