@@ -779,6 +779,13 @@ pub trait Reader {
779
779
self . read_byte ( ) . map ( |i| i as i8 )
780
780
}
781
781
782
+ /// Creates a wrapper around a mutable reference to the reader.
783
+ ///
784
+ /// This is useful to allow applying adaptors while still
785
+ /// retaining ownership of the original value.
786
+ fn by_ref < ' a > ( & ' a mut self ) -> RefReader < ' a , Self > {
787
+ RefReader { inner : self }
788
+ }
782
789
}
783
790
784
791
impl Reader for ~Reader {
@@ -789,6 +796,14 @@ impl<'a> Reader for &'a mut Reader {
789
796
fn read ( & mut self , buf : & mut [ u8 ] ) -> IoResult < uint > { self . read ( buf) }
790
797
}
791
798
799
+ pub struct RefReader < ' a , R > {
800
+ priv inner : & ' a mut R
801
+ }
802
+
803
+ impl < ' a , R : Reader > Reader for RefReader < ' a , R > {
804
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> IoResult < uint > { self . inner . read ( buf) }
805
+ }
806
+
792
807
fn extend_sign ( val : u64 , nbytes : uint ) -> i64 {
793
808
let shift = ( 8 - nbytes) * 8 ;
794
809
( val << shift) as i64 >> shift
@@ -969,6 +984,14 @@ pub trait Writer {
969
984
fn write_i8 ( & mut self , n : i8 ) -> IoResult < ( ) > {
970
985
self . write ( [ n as u8 ] )
971
986
}
987
+
988
+ /// Creates a wrapper around a mutable reference to the writer.
989
+ ///
990
+ /// This is useful to allow applying wrappers while still
991
+ /// retaining ownership of the original value.
992
+ fn by_ref < ' a > ( & ' a mut self ) -> RefWriter < ' a , Self > {
993
+ RefWriter { inner : self }
994
+ }
972
995
}
973
996
974
997
impl Writer for ~Writer {
@@ -981,6 +1004,16 @@ impl<'a> Writer for &'a mut Writer {
981
1004
fn flush ( & mut self ) -> IoResult < ( ) > { self . flush ( ) }
982
1005
}
983
1006
1007
+ pub struct RefWriter < ' a , W > {
1008
+ inner : & ' a mut W
1009
+ }
1010
+
1011
+ impl < ' a , W : Writer > Writer for RefWriter < ' a , W > {
1012
+ fn write ( & mut self , buf : & [ u8 ] ) -> IoResult < ( ) > { self . inner . write ( buf) }
1013
+ fn flush ( & mut self ) -> IoResult < ( ) > { self . inner . flush ( ) }
1014
+ }
1015
+
1016
+
984
1017
pub trait Stream : Reader + Writer { }
985
1018
986
1019
impl < T : Reader + Writer > Stream for T { }
0 commit comments