Skip to content

Commit

Permalink
port changes from upstream master
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Gherghescu <[email protected]>
  • Loading branch information
andrei-ng committed Sep 11, 2023
1 parent 1d90a85 commit f78d322
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 454 deletions.
11 changes: 5 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# IDEs
.idea/
.vscode
Cargo.lock
target/

# Rust specific
/target
**/*.rs.bk
*~
Cargo.lock

.idea/
.vscode
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["ublox", "ublox_derive", "examples/*"]
members = ["ublox", "ublox_derive"]
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]
exclude = ["target"]
members = ["*"]
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Examples

This folder contains usage examples.

These examples use a more recent `rustc` compiler version than the library.

To build the examples, you can override the toolchain used specifically to this folder by doing, e.g.,
```
rustup install 1.70
rustup override set 1.70
```
1 change: 1 addition & 0 deletions ublox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ublox_derive = {path = "../ublox_derive", version = "0.0.4"}
[dev-dependencies]
cpu-time = "1.0.0"
cpuprofiler = "0.0.4"
# Latest depends on clap v4 which requires rustc 1.70
criterion = "0.4.0"
rand = "0.8.5"
serde_json = "1.0.85"
Expand Down
21 changes: 13 additions & 8 deletions ublox/benches/packet_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn profiled() -> Criterion {
Criterion::default().with_profiler(CpuProfiler)
}

#[allow(dead_code)]
fn parse_all<T: UnderlyingBuffer>(mut parser: Parser<T>, data: &[u8], chunk_size: usize) -> usize {
let mut count = 0;
for chunk in data.chunks(chunk_size) {
Expand All @@ -48,20 +49,24 @@ pub fn criterion_benchmark(c: &mut Criterion) {
for chunk in &[99, 100, 101, 256, 512, 1000, 1024] {
c.bench_function(&format!("vec_parse_pos_{}", chunk), |b| {
b.iter(|| {
let data = std::include_bytes!("pos.ubx");
let parser = Parser::default();
assert_eq!(parse_all(parser, data, *chunk), 2801);
// TODO: requires pos.ubx file
// let data = std::include_bytes!("pos.ubx");
// let parser = Parser::default();
// assert_eq!(parse_all(parser, data, *chunk), 2801);
todo!()
})
});
}
for (buf_size, chunk) in &[(256, 100), (256, 256), (256, 512), (256, 1024)] {
let mut underlying = vec![0; *buf_size];
// let mut underlying = vec![0; *buf_size];
c.bench_function(&format!("array_parse_pos_{}_{}", buf_size, chunk), |b| {
b.iter(|| {
let data = std::include_bytes!("pos.ubx");
let underlying = FixedLinearBuffer::new(&mut underlying);
let parser = Parser::new(underlying);
assert_eq!(parse_all(parser, data, *chunk), 2801);
// TODO: requires pos.ubx file
// let data = std::include_bytes!("pos.ubx");
// let underlying = FixedLinearBuffer::new(&mut underlying);
// let parser = Parser::new(underlying);
// assert_eq!(parse_all(parser, data, *chunk), 2801);
todo!()
})
});
}
Expand Down
66 changes: 15 additions & 51 deletions ublox/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<'a> UnderlyingBuffer for FixedLinearBuffer<'a> {
}

fn find(&self, value: u8) -> Option<usize> {
(0..self.len()).find(|&i| self.buffer[i] == value)
(0..self.len()).find(|&i| self[i] == value)
}
}

Expand Down Expand Up @@ -622,12 +622,9 @@ mod test {

let mut dual = DualBuffer::new(&mut buf, &new[..]);
// This should throw
match dual.take(6) {
Err(ParserError::OutOfMemory { required_size }) => {
assert_eq!(required_size, 6);
},
_ => assert!(false),
}
assert!(
matches!(dual.take(6), Err(ParserError::OutOfMemory { required_size }) if required_size == 6)
);
}

#[test]
Expand Down Expand Up @@ -766,12 +763,7 @@ mod test {

let mut it = parser.consume(&bytes);
for _ in 0..5 {
match it.next() {
Some(Ok(PacketRef::AckAck(_packet))) => {
// We're good
},
_ => assert!(false),
}
assert!(matches!(it.next(), Some(Ok(PacketRef::AckAck(_)))));
}
assert!(it.next().is_none());
}
Expand All @@ -786,12 +778,7 @@ mod test {

{
let mut it = parser.consume(&bytes);
match it.next() {
Some(Ok(PacketRef::AckAck(_packet))) => {
// We're good
},
_ => assert!(false),
}
assert!(matches!(it.next(), Some(Ok(PacketRef::AckAck(_)))));
assert!(it.next().is_none());
}
}
Expand Down Expand Up @@ -830,14 +817,9 @@ mod test {

{
let mut it = parser.consume(&bytes[8..]);
match it.next() {
Some(Err(ParserError::OutOfMemory { required_size })) => {
assert_eq!(required_size, bytes.len() - 6);
},
_ => {
assert!(false);
},
}
assert!(
matches!(it.next(), Some(Err(ParserError::OutOfMemory { required_size })) if required_size == bytes.len() - 6)
);
assert!(it.next().is_none());
}

Expand All @@ -846,12 +828,7 @@ mod test {

{
let mut it = parser.consume(&bytes);
match it.next() {
Some(Ok(PacketRef::AckAck(_packet))) => {
// We're good
},
_ => assert!(false),
}
assert!(matches!(it.next(), Some(Ok(PacketRef::AckAck(_)))));
assert!(it.next().is_none());
}
}
Expand Down Expand Up @@ -883,14 +860,7 @@ mod test {
let buffer = FixedLinearBuffer::new(&mut buffer);
let mut parser = Parser::new(buffer);
let mut it = parser.consume(&bytes);
match it.next() {
Some(Ok(PacketRef::CfgNav5(_packet))) => {
// We're good
},
_ => {
assert!(false);
},
}
assert!(matches!(it.next(), Some(Ok(PacketRef::CfgNav5(_)))));
assert!(it.next().is_none());
}

Expand Down Expand Up @@ -920,14 +890,7 @@ mod test {

let mut parser = Parser::default();
let mut it = parser.consume(&bytes);
match it.next() {
Some(Ok(PacketRef::CfgNav5(_packet))) => {
// We're good
},
_ => {
assert!(false);
},
}
assert!(matches!(it.next(), Some(Ok(PacketRef::CfgNav5(_)))));
assert!(it.next().is_none());
}

Expand Down Expand Up @@ -958,7 +921,7 @@ mod test {
assert_eq!(packet.pacc(), 21);
},
_ => {
assert!(false);
panic!()
},
}
match it.next() {
Expand All @@ -967,13 +930,14 @@ mod test {
assert_eq!(packet.pacc(), 18);
},
_ => {
assert!(false);
panic!()
},
}
assert!(it.next().is_none());
}

#[test]
#[allow(clippy::assertions_on_constants)]
fn test_max_payload_len() {
assert!(MAX_PAYLOAD_LEN >= 1240);
}
Expand Down
Loading

0 comments on commit f78d322

Please sign in to comment.