Skip to content

Commit eec0381

Browse files
committed
remove RustDefault
1 parent bff1f83 commit eec0381

6 files changed

Lines changed: 60 additions & 54 deletions

File tree

CODE-REVIEW.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Code Review
2+
3+
## Findings (ordered by severity)
4+
5+
- [High] Signed 24-bit encode/decode is incorrect: decode zero-extends and encode rejects negative values.
6+
- mavlink-core/src/bytes.rs:145
7+
- mavlink-core/src/bytes_mut.rs:121
8+
- Impact: negative `int24` values are parsed as large positives and serialization panics for valid negative values.
9+
- Fix: sign-extend on read and use a negative MIN (-(1 << 23)) on write; consider unit tests for `i24` round-trips.
10+
11+
- [High] Build script requires `git`, mutates the submodule, and ignores non-zero exit statuses.
12+
- mavlink/build/main.rs:13
13+
- mavlink/build/main.rs:19
14+
- mavlink/build/main.rs:35
15+
- Impact: builds can fail in offline/crates.io environments or produce inconsistent definitions; patch failures are silently ignored because only spawn errors are checked.
16+
- Fix: ship patched XML definitions (or pre-generated Rust), avoid VCS operations in `build.rs`, and check `status.success()` for `git` calls if they remain.
17+
18+
- [Medium] `tokio-1` and `embedded` are documented as incompatible but not enforced, and they define duplicate async APIs.
19+
- mavlink-core/src/lib.rs:875
20+
- mavlink-core/src/lib.rs:899
21+
- Impact: enabling both features produces duplicate symbol errors and confusing APIs.
22+
- Fix: add a `compile_error!` guard for `cfg(all(feature = "tokio-1", feature = "embedded"))` or gate one set of functions with `not(feature = "embedded")`.
23+
24+
- [Medium] UDP (sync/async) `recv` loops swallow all errors, and file `recv` ignores non-EOF I/O errors.
25+
- mavlink-core/src/connection/udp.rs:90
26+
- mavlink-core/src/async_connection/udp.rs:92
27+
- mavlink-core/src/connection/file.rs:45
28+
- Impact: persistent I/O errors turn into infinite loops or busy-spins; callers cannot observe disconnects or socket failures.
29+
- Fix: only ignore parse/CRC errors; propagate `MessageReadError::Io` (except maybe `WouldBlock`/`UnexpectedEof` where appropriate).
30+
31+
- [Low] `PeekReader`/`AsyncPeekReader` rejects exact buffer-size reads even though docs say “more than BUFFER_SIZE”.
32+
- mavlink-core/src/peek_reader.rs:141
33+
- mavlink-core/src/async_peek_reader.rs:139
34+
- Impact: `peek_exact(BUFFER_SIZE)` panics; API behavior does not match documentation.
35+
- Fix: change `< BUFFER_SIZE` to `<= BUFFER_SIZE` and update docs/tests.
36+
37+
- [Low] Address format docs and examples use `udpbcast`, but the parser accepts `udpcast`.
38+
- mavlink-core/src/connectable.rs:78
39+
- mavlink-core/src/async_connection/mod.rs:91
40+
- mavlink/examples/mavlink-dump/src/main.rs:10
41+
- Impact: user confusion and copy/paste failures.
42+
- Fix: accept both or standardize documentation and CLI help on one string.
43+
44+
## Simplification / existing-crate opportunities
45+
46+
- Replace custom `bytes`/`bytes_mut` helpers with `byteorder::ByteOrder` and/or `bytes::Buf`/`BufMut` for most primitives, keeping a small custom helper only for `u24/i24`. This reduces bespoke parsing code and risk (the current i24 bug is a good example).
47+
- mavlink-core/src/bytes.rs
48+
- mavlink-core/src/bytes_mut.rs
49+
- mavlink-bindgen/src/parser.rs:806
50+
51+
- Drop `utils::RustDefault` in favor of `Default` (arrays implement `Default` on Rust 1.80). Update codegen to use `#[serde(default)]` instead of a custom default function.
52+
- mavlink-core/src/utils.rs
53+
- mavlink-bindgen/src/parser.rs:752
54+
- mavlink/src/lib.rs:68
55+
56+
- Consider splitting the 2300-line `mavlink-core/src/lib.rs` into focused modules (frame structs, parsing, write APIs, connection glue) or using small macros for the repeated v1/v2 + sync/async variants to reduce duplication and review surface.
57+
- mavlink-core/src/lib.rs

mavlink-bindgen/src/parser.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,7 @@ impl MavMessage {
746746
// If sent by an implementation that doesn't have the extensions fields
747747
// then the recipient will see zero values for the extensions fields.
748748
let serde_default = if field.is_extension {
749-
if field.enumtype.is_some() {
750-
quote!(#[cfg_attr(feature = "serde", serde(default))])
751-
} else {
752-
quote!(#[cfg_attr(feature = "serde", serde(default = "crate::RustDefault::rust_default"))])
753-
}
749+
quote!(#[cfg_attr(feature = "serde", serde(default))])
754750
} else {
755751
quote!()
756752
};

mavlink-core/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ use core::result::Result;
9090
use std::io::{Read, Write};
9191

9292
pub mod utils;
93-
#[allow(unused_imports)]
94-
use utils::{remove_trailing_zeroes, RustDefault};
9593

9694
#[cfg(feature = "serde")]
9795
use serde::{Deserialize, Serialize};

mavlink-core/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ impl<const N: usize> From<&str> for CharArray<N> {
115115
}
116116
}
117117

118-
impl<const N: usize> crate::utils::RustDefault for CharArray<N> {
118+
impl<const N: usize> Default for CharArray<N> {
119119
#[inline(always)]
120-
fn rust_default() -> Self {
120+
fn default() -> Self {
121121
Self::new([0u8; N])
122122
}
123123
}

mavlink-core/src/utils.rs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,6 @@ pub fn remove_trailing_zeroes(data: &[u8]) -> usize {
1616
len
1717
}
1818

19-
/// A trait very similar to [`Default`] but is only implemented for the equivalent Rust types to
20-
/// `MavType`s.
21-
///
22-
/// This is only needed because rust doesn't currently implement `Default` for arrays
23-
/// of all sizes. In particular this trait is only ever used when the "serde" feature is enabled.
24-
/// For more information, check out [this issue](https://users.rust-lang.org/t/issue-for-derives-for-arrays-greater-than-size-32/59055/3).
25-
pub trait RustDefault: Copy {
26-
fn rust_default() -> Self;
27-
}
28-
29-
impl<T: RustDefault, const N: usize> RustDefault for [T; N] {
30-
#[inline(always)]
31-
fn rust_default() -> Self {
32-
let val: T = RustDefault::rust_default();
33-
[val; N]
34-
}
35-
}
36-
37-
macro_rules! impl_rust_default {
38-
($($t:ty => $val:expr),* $(,)?) => {
39-
$(impl RustDefault for $t {
40-
#[inline(always)]
41-
fn rust_default() -> Self { $val }
42-
})*
43-
};
44-
}
45-
46-
impl_rust_default! {
47-
u8 => 0,
48-
i8 => 0,
49-
u16 => 0,
50-
i16 => 0,
51-
u32 => 0,
52-
i32 => 0,
53-
u64 => 0,
54-
i64 => 0,
55-
f32 => 0.0,
56-
f64 => 0.0,
57-
char => '\0',
58-
}
59-
6019
#[cfg(test)]
6120
mod tests {
6221
use super::*;

mavlink/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ include!(concat!(env!("OUT_DIR"), "/mod.rs"));
6363

6464
pub use mavlink_core::*;
6565

66-
#[cfg(feature = "emit-extensions")]
67-
#[allow(unused_imports)]
68-
pub(crate) use mavlink_core::utils::RustDefault;
69-
7066
#[cfg(feature = "serde")]
7167
#[allow(unused_imports)]
7268
pub(crate) use mavlink_core::utils::nulstr;

0 commit comments

Comments
 (0)