Skip to content

Commit fe69513

Browse files
committed
fix: apply formatter and linter fixes
- Fix formatting and linting issues across platform modules - Improve code style consistency in wrt-foundation and wrt-platform - Address clippy warnings in type conversion modules
1 parent fcb4af8 commit fe69513

File tree

13 files changed

+124
-196
lines changed

13 files changed

+124
-196
lines changed

wrt-component/src/type_conversion/bidirectional.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,8 @@ pub fn core_value_to_types_componentvalue(
840840
wrt_foundation::values::Value::I64(v) => Ok(ComponentValue::S64(*v)),
841841
wrt_foundation::values::Value::F32(v) => Ok(ComponentValue::F32(*v)),
842842
wrt_foundation::values::Value::F64(v) => Ok(ComponentValue::F64(*v)),
843-
wrt_foundation::values::Value::Ref(v) => Ok(ComponentValue::U32(*v)), /* Map reference
844-
* to U32 */
843+
wrt_foundation::values::Value::Ref(v) => Ok(ComponentValue::U32(*v)), // Map reference
844+
// to U32
845845
_ => Err(Error::new(
846846
ErrorCategory::Type,
847847
codes::CONVERSION_ERROR,

wrt-foundation/src/bounded.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,7 @@ where
602602
})?;
603603

604604
self.handler.write_data(offset, &item_bytes_buffer[..bytes_written]).map_err(|e| {
605-
BoundedError::new(
606-
BoundedErrorKind::SliceError,
607-
e.message().unwrap_or("Write data failed"),
608-
)
605+
BoundedError::new(BoundedErrorKind::SliceError, "Write data failed: error occurred")
609606
})?;
610607

611608
self.length += 1;
@@ -1030,10 +1027,7 @@ where
10301027
})?;
10311028

10321029
self.provider.write_data(offset, &item_bytes_buffer[..bytes_written]).map_err(|e| {
1033-
BoundedError::new(
1034-
BoundedErrorKind::SliceError,
1035-
e.message().unwrap_or("Write data failed"),
1036-
)
1030+
BoundedError::new(BoundedErrorKind::SliceError, "Write data failed: error occurred")
10371031
})?;
10381032

10391033
self.length += 1;
@@ -2854,11 +2848,8 @@ where
28542848
{
28552849
fn serialized_size(&self) -> usize {
28562850
// Length (u32) + checksum + items
2857-
4 + self.checksum.serialized_size() + (self.length * if self.length > 0 {
2858-
T::default().serialized_size()
2859-
} else {
2860-
0
2861-
})
2851+
4 + self.checksum.serialized_size()
2852+
+ (self.length * if self.length > 0 { T::default().serialized_size() } else { 0 })
28622853
}
28632854

28642855
fn to_bytes_with_provider<'a, PStream: crate::MemoryProvider>(
@@ -3674,8 +3665,9 @@ impl<const N_BYTES: usize, P: MemoryProvider + Default + Clone + PartialEq + Eq>
36743665
}
36753666
}
36763667

3677-
// Note: This impl block was removed due to overlapping type bounds with the main impl block.
3678-
// All necessary methods are already defined in the main impl block.
3668+
// Note: This impl block was removed due to overlapping type bounds with the
3669+
// main impl block. All necessary methods are already defined in the main impl
3670+
// block.
36793671

36803672
// ... (other BoundedVec impl methods, make sure to use `Error::` where it was
36813673
// `Error::` before) For example, in BoundedVec::get:
@@ -3760,7 +3752,8 @@ where
37603752
P: MemoryProvider + Clone + PartialEq + Eq,
37613753
{
37623754
// This impl block provides methods with additional constraints
3763-
// The verify_item_checksum_at_offset method is already defined in the main impl block
3755+
// The verify_item_checksum_at_offset method is already defined in the main impl
3756+
// block
37643757
}
37653758

37663759
// Alloc-dependent methods for BoundedString

wrt-foundation/src/prelude.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ pub use hashbrown::HashMap;
6161
pub use wrt_error::prelude::*;
6262
pub use wrt_error::{codes, kinds, Error, ErrorCategory, Result};
6363

64+
// Feature-gated re-exports that can't be included in the main use block
65+
#[cfg(feature = "alloc")]
66+
pub use crate::component_builder::{
67+
ComponentTypeBuilder, ExportBuilder, ImportBuilder, NamespaceBuilder,
68+
};
6469
// Re-export from wrt_sync, only if the feature is active
6570
// #[cfg(feature = "wrt-sync")] // Or a more specific feature if wrt-sync is always a dep
6671

@@ -132,7 +137,3 @@ pub use crate::{
132137
SafeMemoryHandler,
133138
SafeSlice,
134139
};
135-
136-
// Feature-gated re-exports that can't be included in the main use block
137-
#[cfg(feature = "alloc")]
138-
pub use crate::component_builder::{ComponentTypeBuilder, ExportBuilder, ImportBuilder, NamespaceBuilder};

wrt-foundation/src/traits.rs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,6 @@ use alloc::vec::Vec;
3434
// This module provides common traits used for type conversions between format
3535
// and runtime representations.
3636

37-
/// A writer that counts the number of bytes written without storing them.
38-
/// Used for calculating serialized sizes.
39-
struct CountingWriter {
40-
count: usize,
41-
}
42-
43-
impl CountingWriter {
44-
fn new() -> Self {
45-
Self { count: 0 }
46-
}
47-
48-
fn bytes_written(&self) -> usize {
49-
self.count
50-
}
51-
}
52-
53-
impl BytesWriter for CountingWriter {
54-
fn write_byte(&mut self, _byte: u8) -> WrtResult<()> {
55-
self.count += 1;
56-
Ok(())
57-
}
58-
59-
fn write_all(&mut self, bytes: &[u8]) -> WrtResult<()> {
60-
self.count += bytes.len();
61-
Ok(())
62-
}
63-
}
64-
6537
/// Trait for types that can be converted from a format representation
6638
pub trait FromFormat<T> {
6739
/// Convert from a format representation
@@ -174,16 +146,9 @@ impl Checksummable for alloc::string::String {
174146
pub trait ToBytes: Sized {
175147
/// Returns the size in bytes required to serialize this type.
176148
/// This should be a constant for fixed-size types.
177-
/// Default implementation uses a temporary buffer to calculate size.
149+
/// Default implementation returns 0 - types should override this.
178150
fn serialized_size(&self) -> usize {
179-
// Default implementation - serialize to a counting writer to get size
180-
let mut counter = CountingWriter::new();
181-
let provider = NoStdProvider;
182-
if self.to_bytes_with_provider(&mut WriteStream::new(&mut counter), &provider).is_ok() {
183-
counter.bytes_written()
184-
} else {
185-
0 // Fallback if serialization fails
186-
}
151+
0 // Default fallback - should be overridden by implementations
187152
}
188153

189154
/// Serializes the type into a byte stream using a provided memory stream
@@ -647,7 +612,7 @@ impl<T: ToBytes> ToBytes for Option<T> {
647612
fn serialized_size(&self) -> usize {
648613
match self {
649614
Some(value) => 1 + value.serialized_size(), // 1 byte for tag + value size
650-
None => 1, // 1 byte for tag
615+
None => 1, // 1 byte for tag
651616
}
652617
}
653618

wrt-foundation/src/validation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
extern crate alloc;
1919

2020
// Added BoundedVec for tests
21+
use wrt_error::ErrorCategory;
22+
2123
#[cfg(test)]
2224
use crate::bounded::BoundedVec;
2325
use crate::prelude::{Debug /* Removed format, ToString as _ToString */};
24-
use wrt_error::ErrorCategory;
2526
#[cfg(test)]
2627
use crate::safe_memory::NoStdProvider;
2728
#[cfg(all(feature = "alloc", not(feature = "std")))]

wrt-platform/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,14 @@ pub use qnx_partition::{
117117
};
118118
#[cfg(all(feature = "platform-qnx", target_os = "nto"))]
119119
pub use qnx_sync::{QnxFutex, QnxFutexBuilder, QnxSyncPriority};
120-
120+
pub use sync::{FutexLike, SpinFutex, SpinFutexBuilder, TimeoutResult}; /* FutexLike is always available */
121+
// Re-export core error type (also available via prelude)
122+
pub use wrt_error::Error;
121123
// Export Zephyr specific implementations if enabled
122124
#[cfg(feature = "platform-zephyr")]
123125
pub use zephyr_memory::{ZephyrAllocator, ZephyrAllocatorBuilder, ZephyrMemoryFlags};
124126
#[cfg(feature = "platform-zephyr")]
125-
pub use zephyr_sync::{ZephyrFutex, ZephyrFutexBuilder, ZephyrSemaphoreFutex};
126-
pub use sync::{FutexLike, SpinFutex, SpinFutexBuilder, TimeoutResult}; /* FutexLike is always available */
127-
// Re-export core error type (also available via prelude)
128-
pub use wrt_error::Error; // This is fine as wrt_error::Error is always available
127+
pub use zephyr_sync::{ZephyrFutex, ZephyrFutexBuilder, ZephyrSemaphoreFutex}; // This is fine as wrt_error::Error is always available
129128

130129
#[cfg(test)]
131130
#[allow(clippy::panic)] // Allow panics in the test module

wrt-platform/src/linux_memory.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
1818
use core::ptr::{self, NonNull};
1919

20-
// Safety: NonNull<u8> is safe to send between threads as it's just a pointer wrapper
20+
// Safety: NonNull<u8> is safe to send between threads as it's just a pointer
21+
// wrapper
2122
unsafe impl Send for LinuxAllocator {}
2223
unsafe impl Sync for LinuxAllocator {}
2324

wrt-platform/src/linux_memory_arm64_mte.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
1818
use core::ptr::{self, NonNull};
1919

20-
// Safety: NonNull<u8> is safe to send between threads as it's just a pointer wrapper
20+
// Safety: NonNull<u8> is safe to send between threads as it's just a pointer
21+
// wrapper
2122
unsafe impl Send for LinuxArm64MteAllocator {}
2223
unsafe impl Sync for LinuxArm64MteAllocator {}
2324

wrt-platform/src/linux_sync.rs

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// Licensed under the MIT license.
99
// SPDX-License-Identifier: MIT
1010

11-
//! Linux-specific `FutexLike` implementation using direct futex syscalls without
12-
//! libc.
11+
//! Linux-specific `FutexLike` implementation using direct futex syscalls
12+
//! without libc.
1313
//!
1414
//! This implementation provides wait/notify synchronization using Linux futex
1515
//! system calls directly, supporting no_std/no_alloc environments.
@@ -51,10 +51,7 @@ struct TimeSpec {
5151
impl TimeSpec {
5252
/// Create a new TimeSpec from Duration
5353
fn from_duration(duration: Duration) -> Self {
54-
Self {
55-
tv_sec: duration.as_secs() as i64,
56-
tv_nsec: duration.subsec_nanos() as i64,
57-
}
54+
Self { tv_sec: duration.as_secs() as i64, tv_nsec: duration.subsec_nanos() as i64 }
5855
}
5956

6057
/// Create a zero timeout (immediate)
@@ -75,17 +72,15 @@ pub struct LinuxFutex {
7572
_padding: [u8; 60], // 64 - sizeof(AtomicU32)
7673
}
7774

78-
// Safety: LinuxFutex only contains AtomicU32 and padding, which are safe to send/sync
75+
// Safety: LinuxFutex only contains AtomicU32 and padding, which are safe to
76+
// send/sync
7977
unsafe impl Send for LinuxFutex {}
8078
unsafe impl Sync for LinuxFutex {}
8179

8280
impl LinuxFutex {
8381
/// Creates a new `LinuxFutex` with the given initial value.
8482
pub fn new(initial_value: u32) -> Self {
85-
Self {
86-
value: AtomicU32::new(initial_value),
87-
_padding: [0; 60],
88-
}
83+
Self { value: AtomicU32::new(initial_value), _padding: [0; 60] }
8984
}
9085

9186
/// Direct syscall implementation of futex
@@ -135,14 +130,7 @@ impl LinuxFutex {
135130
// Call futex wake
136131
// SAFETY: We're calling futex wake with valid parameters.
137132
let result = unsafe {
138-
Self::futex(
139-
addr,
140-
FUTEX_WAKE_PRIVATE,
141-
count,
142-
core::ptr::null(),
143-
core::ptr::null(),
144-
0,
145-
)
133+
Self::futex(addr, FUTEX_WAKE_PRIVATE, count, core::ptr::null(), core::ptr::null(), 0)
146134
};
147135

148136
if result >= 0 {
@@ -203,25 +191,14 @@ impl FutexLike for LinuxFutex {
203191
// SAFETY: We're calling futex with valid parameters. addr points to self.value,
204192
// which is valid for the lifetime of self.
205193
let result = unsafe {
206-
Self::futex(
207-
addr,
208-
FUTEX_WAIT_PRIVATE,
209-
expected,
210-
timeout_ptr,
211-
core::ptr::null(),
212-
0,
213-
)
194+
Self::futex(addr, FUTEX_WAIT_PRIVATE, expected, timeout_ptr, core::ptr::null(), 0)
214195
};
215196

216197
match result {
217198
0 => Ok(()), // Woken up by notify
218199
-110 => {
219200
// ETIMEDOUT - convert to system error as per trait contract
220-
Err(Error::new(
221-
ErrorCategory::System,
222-
codes::SYSTEM_ERROR,
223-
"Futex wait timed out",
224-
))
201+
Err(Error::new(ErrorCategory::System, codes::SYSTEM_ERROR, "Futex wait timed out"))
225202
}
226203
-11 => {
227204
// EAGAIN - value changed before we could wait, this is success
@@ -247,10 +224,6 @@ impl FutexLike for LinuxFutex {
247224

248225
impl fmt::Display for LinuxFutex {
249226
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250-
write!(
251-
f,
252-
"LinuxFutex({})",
253-
self.value.load(core::sync::atomic::Ordering::Relaxed)
254-
)
227+
write!(f, "LinuxFutex({})", self.value.load(core::sync::atomic::Ordering::Relaxed))
255228
}
256-
}
229+
}

0 commit comments

Comments
 (0)