Skip to content

Commit eb95211

Browse files
committed
Implement RustDefault for CharArray
1 parent 946d9f3 commit eb95211

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

mavlink-core/src/types.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use arbitrary::{Arbitrary, Unstructured};
2020
/// let ca = CharArray::new(data);
2121
/// assert_eq!(ca.to_str(), "HELLO");
2222
/// ```
23-
#[derive(Debug, PartialEq, Clone)]
23+
#[derive(Debug, PartialEq, Clone, Copy)]
2424
#[cfg_attr(feature = "serde", derive(Serialize))]
2525
#[cfg_attr(feature = "serde", serde(transparent))]
2626
pub struct CharArray<const N: usize> {
@@ -57,7 +57,7 @@ impl<const N: usize> CharArray<N> {
5757
/// Returns the string stopping at the first null byte and if the string is not valid utf8
5858
/// the returned string will be empty.
5959
pub fn to_str(&self) -> &str {
60-
std::str::from_utf8(&self.data[..self.str_len]).unwrap_or("")
60+
core::str::from_utf8(&self.data[..self.str_len]).unwrap_or("")
6161
}
6262
}
6363

@@ -96,6 +96,13 @@ impl<const N: usize> From<CharArray<N>> for [u8; N] {
9696
}
9797
}
9898

99+
impl<const N: usize> crate::utils::RustDefault for CharArray<N> {
100+
#[inline(always)]
101+
fn rust_default() -> Self {
102+
Self::new([0u8; N])
103+
}
104+
}
105+
99106
#[cfg(feature = "serde")]
100107
impl<'de, const N: usize> Deserialize<'de> for CharArray<N> {
101108
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>

0 commit comments

Comments
 (0)