Skip to content

Commit 020d8df

Browse files
authored
rust: add Frome<Frame> impls for Bytes/BytesMut (#1508)
# Description @swift-nav/algint-team <!-- Changes proposed in this PR --> # API compatibility Does this change introduce a API compatibility risk? <!-- Provide a short explanation why or why not --> ## API compatibility plan If the above is "Yes", please detail the compatibility (or migration) plan: <!-- Provide a short explanation plan here --> # JIRA Reference https://swift-nav.atlassian.net/browse/BOARD-XXXX
1 parent 076a244 commit 020d8df

File tree

34 files changed

+5305
-1087
lines changed

34 files changed

+5305
-1087
lines changed

rust/sbp/examples/serial.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::time::Duration;
77

88
use serialport::prelude::*;
99

10-
use sbp::{iter_messages, Sbp};
10+
use sbp::{Sbp, iter_messages};
1111

1212
fn main() {
1313
let s = SerialPortSettings {

rust/sbp/src/de.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
time::{Duration, Instant},
55
};
66

7-
use bytes::{Buf, BytesMut};
7+
use bytes::{Buf, Bytes, BytesMut};
88
use dencode::FramedRead;
99

1010
#[cfg(feature = "async")]
@@ -360,6 +360,18 @@ impl Frame {
360360
}
361361
}
362362

363+
impl From<Frame> for BytesMut {
364+
fn from(Frame(buf): Frame) -> Self {
365+
buf
366+
}
367+
}
368+
369+
impl From<Frame> for Bytes {
370+
fn from(Frame(buf): Frame) -> Self {
371+
buf.into()
372+
}
373+
}
374+
363375
struct TimeoutDecoderImpl<D> {
364376
timeout_duration: Duration,
365377
last_msg_received: Instant,

rust/sbp/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub use messages::Sbp;
193193
pub use crate::messages::SbpMessage;
194194

195195
#[doc(inline)]
196-
pub use ser::{to_vec, to_writer, Error as SerializeError, SbpEncoder};
196+
pub use ser::{Error as SerializeError, SbpEncoder, to_vec, to_writer};
197197

198198
#[doc(inline)]
199199
pub use de::{Error as DeserializeError, *};

rust/sbp/src/messages/invalid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use bytes::{Buf, BufMut};
99

10-
use crate::{de::CrcError, messages::SbpMsgParseError, wire_format::WireFormat, SbpMessage};
10+
use crate::{SbpMessage, de::CrcError, messages::SbpMsgParseError, wire_format::WireFormat};
1111

1212
/// Invalid messages occur when either the frame or message payload doesn't have enough bytes or
1313
/// the CRC does not match the messages payload. If the message is well formed with a message

rust/sbp/src/messages/unknown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use bytes::{Buf, BufMut};
44

5-
use crate::{wire_format::WireFormat, SbpMessage};
5+
use crate::{SbpMessage, wire_format::WireFormat};
66

77
/// The message returned by the parser when the message type does not correspond to a known message.
88
///

rust/sbp/src/sbp_iter_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ mod swiftnav_impl {
120120
use swiftnav::time::GpsTime;
121121

122122
use crate::{
123+
Frame, Sbp,
123124
messages::SbpMessage,
124125
time::{GpsTimeError, MessageTime, RoverTime},
125-
Frame, Sbp,
126126
};
127127

128128
/// See [SbpIterExt::with_rover_time] for more information.

rust/sbp/src/ser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use bytes::BytesMut;
55
use dencode::{Encoder, FramedWrite, IterSinkExt};
66

77
use crate::wire_format::WireFormat;
8-
use crate::{Sbp, SbpMessage};
98
use crate::{BUFLEN, MAX_PAYLOAD_LEN, PREAMBLE};
9+
use crate::{Sbp, SbpMessage};
1010

1111
/// Serialize the given message into the IO stream.
1212
///

rust/sbp/tests/integration/auto_check_sbp_file_io_msg_fileio_remove.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ fn test_auto_check_sbp_file_io_msg_fileio_remove() {
4747
);
4848
assert_eq!(
4949
msg.filename.as_bytes(),
50-
&[47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0],
50+
&[
51+
47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0
52+
],
5153
"incorrect value for msg.filename, expected string '{:?}', is '{:?}'",
52-
&[47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0],
54+
&[
55+
47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0
56+
],
5357
msg.filename.as_bytes()
5458
);
5559
}
@@ -105,9 +109,13 @@ fn test_json2sbp_auto_check_sbp_file_io_msg_fileio_remove() {
105109
);
106110
assert_eq!(
107111
msg.filename.as_bytes(),
108-
&[47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0],
112+
&[
113+
47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0
114+
],
109115
"incorrect value for msg.filename, expected string '{:?}', is '{:?}'",
110-
&[47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0],
116+
&[
117+
47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0
118+
],
111119
msg.filename.as_bytes()
112120
);
113121
}
@@ -171,9 +179,13 @@ fn test_sbp2json_auto_check_sbp_file_io_msg_fileio_remove() {
171179
);
172180
assert_eq!(
173181
msg.filename.as_bytes(),
174-
&[47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0],
182+
&[
183+
47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0
184+
],
175185
"incorrect value for msg.filename, expected string '{:?}', is '{:?}'",
176-
&[47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0],
186+
&[
187+
47, 112, 97, 116, 104, 47, 116, 111, 47, 102, 105, 108, 101, 0
188+
],
177189
msg.filename.as_bytes()
178190
);
179191
}

rust/sbp/tests/integration/auto_check_sbp_linux_msg_linux_cpu_state.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ fn test_auto_check_sbp_linux_msg_linux_cpu_state() {
9090
);
9191
assert_eq!(
9292
msg.tname.as_bytes(),
93-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
93+
&[
94+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
95+
],
9496
"incorrect value for msg.tname, expected string '{:?}', is '{:?}'",
95-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
97+
&[
98+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
99+
],
96100
msg.tname.as_bytes()
97101
);
98102
}
@@ -188,9 +192,13 @@ fn test_json2sbp_auto_check_sbp_linux_msg_linux_cpu_state() {
188192
);
189193
assert_eq!(
190194
msg.tname.as_bytes(),
191-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
195+
&[
196+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
197+
],
192198
"incorrect value for msg.tname, expected string '{:?}', is '{:?}'",
193-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
199+
&[
200+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
201+
],
194202
msg.tname.as_bytes()
195203
);
196204
}
@@ -297,9 +305,13 @@ fn test_sbp2json_auto_check_sbp_linux_msg_linux_cpu_state() {
297305
);
298306
assert_eq!(
299307
msg.tname.as_bytes(),
300-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
308+
&[
309+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
310+
],
301311
"incorrect value for msg.tname, expected string '{:?}', is '{:?}'",
302-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
312+
&[
313+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
314+
],
303315
msg.tname.as_bytes()
304316
);
305317
}

rust/sbp/tests/integration/auto_check_sbp_linux_msg_linux_cpu_state_dep_a.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ fn test_auto_check_sbp_linux_msg_linux_cpu_state_dep_a() {
7979
);
8080
assert_eq!(
8181
msg.tname.as_bytes(),
82-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
82+
&[
83+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
84+
],
8385
"incorrect value for msg.tname, expected string '{:?}', is '{:?}'",
84-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
86+
&[
87+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
88+
],
8589
msg.tname.as_bytes()
8690
);
8791
}
@@ -167,9 +171,13 @@ fn test_json2sbp_auto_check_sbp_linux_msg_linux_cpu_state_dep_a() {
167171
);
168172
assert_eq!(
169173
msg.tname.as_bytes(),
170-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
174+
&[
175+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
176+
],
171177
"incorrect value for msg.tname, expected string '{:?}', is '{:?}'",
172-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
178+
&[
179+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
180+
],
173181
msg.tname.as_bytes()
174182
);
175183
}
@@ -265,9 +273,13 @@ fn test_sbp2json_auto_check_sbp_linux_msg_linux_cpu_state_dep_a() {
265273
);
266274
assert_eq!(
267275
msg.tname.as_bytes(),
268-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
276+
&[
277+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
278+
],
269279
"incorrect value for msg.tname, expected string '{:?}', is '{:?}'",
270-
&[112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0],
280+
&[
281+
112, 114, 111, 99, 101, 115, 115, 45, 110, 97, 109, 101, 0, 0, 0
282+
],
271283
msg.tname.as_bytes()
272284
);
273285
}

0 commit comments

Comments
 (0)