File tree 2 files changed +52
-0
lines changed
2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,19 @@ pub trait BinWrite {
86
86
self . write_le_args ( writer, Self :: Args :: args ( ) )
87
87
}
88
88
89
+ /// Write `Self` to the writer assuming native-endian byte order.
90
+ ///
91
+ /// # Errors
92
+ ///
93
+ /// If writing fails, an [`Error`](crate::Error) variant will be returned.
94
+ #[ inline]
95
+ fn write_ne < W : Write + Seek > ( & self , writer : & mut W ) -> BinResult < ( ) >
96
+ where
97
+ for < ' a > Self :: Args < ' a > : Required ,
98
+ {
99
+ self . write_ne_args ( writer, Self :: Args :: args ( ) )
100
+ }
101
+
89
102
/// Write `Self` to the writer using the given arguments.
90
103
///
91
104
/// # Errors
@@ -129,6 +142,21 @@ pub trait BinWrite {
129
142
self . write_options ( writer, Endian :: Little , args)
130
143
}
131
144
145
+ /// Write `Self` to the writer, assuming native-endian byte order, using the
146
+ /// given arguments.
147
+ ///
148
+ /// # Errors
149
+ ///
150
+ /// If reading fails, an [`Error`](crate::Error) variant will be returned.
151
+ #[ inline]
152
+ fn write_ne_args < W : Write + Seek > (
153
+ & self ,
154
+ writer : & mut W ,
155
+ args : Self :: Args < ' _ > ,
156
+ ) -> BinResult < ( ) > {
157
+ self . write_options ( writer, Endian :: NATIVE , args)
158
+ }
159
+
132
160
/// Write `Self` to the writer using the given [`Endian`] and
133
161
/// arguments.
134
162
///
Original file line number Diff line number Diff line change @@ -75,6 +75,30 @@ fn non_zero() {
75
75
) ;
76
76
}
77
77
78
+ #[ test]
79
+ fn native_endian ( ) {
80
+ #[ derive( BinWrite ) ]
81
+ struct Test ( u16 ) ;
82
+
83
+ #[ derive( BinWrite ) ]
84
+ #[ bw( import( mul: u16 ) ) ]
85
+ struct TestArgs ( #[ bw( map = |val| mul * * val) ] u16 ) ;
86
+
87
+ let mut output = binrw:: io:: Cursor :: new ( vec ! [ ] ) ;
88
+ Test ( 1 ) . write_ne ( & mut output) . unwrap ( ) ;
89
+ #[ cfg( target_endian = "big" ) ]
90
+ assert_eq ! ( output. into_inner( ) , b"\0 \x01 " ) ;
91
+ #[ cfg( target_endian = "little" ) ]
92
+ assert_eq ! ( output. into_inner( ) , b"\x01 \0 " ) ;
93
+
94
+ let mut output = binrw:: io:: Cursor :: new ( vec ! [ ] ) ;
95
+ TestArgs ( 2 ) . write_ne_args ( & mut output, ( 2 , ) ) . unwrap ( ) ;
96
+ #[ cfg( target_endian = "big" ) ]
97
+ assert_eq ! ( output. into_inner( ) , b"\0 \x04 " ) ;
98
+ #[ cfg( target_endian = "little" ) ]
99
+ assert_eq ! ( output. into_inner( ) , b"\x04 \0 " ) ;
100
+ }
101
+
78
102
#[ test]
79
103
fn option ( ) {
80
104
compare ! ( Some ( 1_i32 ) , Endian :: Big , b"\0 \0 \0 \x01 " ) ;
You can’t perform that action at this time.
0 commit comments