diff --git a/.vscode/rustfmt.sh b/.vscode/rustfmt.sh new file mode 100755 index 00000000..b0481d52 --- /dev/null +++ b/.vscode/rustfmt.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +bazel run @score_tooling//format_checker:rustfmt_with_policies diff --git a/.vscode/settings.json b/.vscode/settings.json index e38f9892..77e1871b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -110,5 +110,8 @@ "rust-analyzer.cargo.cfgs": [ "!miri" ], - "rust-analyzer.check.command": "clippy" + "rust-analyzer.check.command": "clippy", + "rust-analyzer.rustfmt.overrideCommand": [ + "${workspaceFolder}/.vscode/rustfmt.sh" + ] } diff --git a/MODULE.bazel b/MODULE.bazel index fe4fadd1..2bf58f74 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -29,7 +29,7 @@ bazel_dep(name = "platforms", version = "1.0.0") bazel_dep(name = "score_bazel_platforms", version = "0.0.2") bazel_dep(name = "score_docs_as_code", version = "2.0.2") bazel_dep(name = "score_tooling", version = "1.0.4") -bazel_dep(name = "score_rust_policies", version = "0.0.2") +bazel_dep(name = "score_rust_policies", version = "0.0.3") bazel_dep(name = "score_process", version = "1.3.1", dev_dependency = True) bazel_dep(name = "score_platform", version = "0.4.2", dev_dependency = True) # This is main score repo @@ -47,7 +47,7 @@ bazel_dep(name = "score_crates") # Overrides git_override( module_name = "score_tooling", - commit = "f484bc23485b0880d8d86e96b8f25d7650889089", #until 1.0.5 is released + commit = "612d6f180a9bb6338de5f0e6667fcf83068d9c37", #until 1.0.5 is released remote = "https://github.com/eclipse-score/tooling.git", ) diff --git a/rustfmt.toml b/rustfmt.toml index a8cd55ec..85b6056b 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,8 +1,4 @@ -# rust formatter rules. -# check configuration fields here: https://rust-lang.github.io/rustfmt/?version=v1.6.0&search= - - -match_block_trailing_comma = true -max_width = 150 -tab_spaces = 4 -use_field_init_shorthand = true +# `rustfmt` should not be used with local configuration. +# Use Bazel target for formatting. +# E.g., `bazel run //:format.fix_Rust_with_rustfmt` +DO_NOT_USE_LOCAL_RUSTFMT_TOML = false diff --git a/src/containers/fixed_capacity/string.rs b/src/containers/fixed_capacity/string.rs index ccce602e..a52119bb 100644 --- a/src/containers/fixed_capacity/string.rs +++ b/src/containers/fixed_capacity/string.rs @@ -38,7 +38,10 @@ impl FixedCapacityString { /// - Panics if the memory allocation fails. #[must_use] pub fn new(capacity: usize) -> Self { - assert!(capacity <= u32::MAX as usize, "FixedCapacityString can hold at most u32::MAX bytes"); + assert!( + capacity <= u32::MAX as usize, + "FixedCapacityString can hold at most u32::MAX bytes" + ); Self { inner: GenericString::new(capacity as u32), } diff --git a/src/containers/generic/queue.rs b/src/containers/generic/queue.rs index cdde8f5e..c037d86b 100644 --- a/src/containers/generic/queue.rs +++ b/src/containers/generic/queue.rs @@ -16,8 +16,8 @@ use core::mem::needs_drop; use core::ops::Range; use core::ptr; -use crate::InsufficientCapacity; use crate::storage::Storage; +use crate::InsufficientCapacity; #[repr(C)] pub struct GenericQueue> { diff --git a/src/containers/generic/string.rs b/src/containers/generic/string.rs index f32656e7..387ccb90 100644 --- a/src/containers/generic/string.rs +++ b/src/containers/generic/string.rs @@ -16,8 +16,8 @@ use core::ops; use core::str; use super::vec::GenericVec; -use crate::InsufficientCapacity; use crate::storage::Storage; +use crate::InsufficientCapacity; /// A UTF-8 encoded string which is generic over its storage method. #[repr(transparent)] diff --git a/src/containers/generic/vec.rs b/src/containers/generic/vec.rs index ec9f1300..6da6f50b 100644 --- a/src/containers/generic/vec.rs +++ b/src/containers/generic/vec.rs @@ -17,8 +17,8 @@ use core::mem::needs_drop; use core::ops; use core::ptr; -use crate::InsufficientCapacity; use crate::storage::Storage; +use crate::InsufficientCapacity; #[repr(C)] pub struct GenericVec> { @@ -143,12 +143,14 @@ impl> GenericVec { /// If the vector has sufficient spare capacity, the operation succeeds and a reference to those elements is returned; /// otherwise, `Err(InsufficientCapacity)` is returned. pub fn extend_from_slice(&mut self, other: &[T]) -> Result<&mut [T], InsufficientCapacity> { - let new_len = (self.len as usize).checked_add(other.len()).ok_or(InsufficientCapacity)?; + let new_len = (self.len as usize) + .checked_add(other.len()) + .ok_or(InsufficientCapacity)?; if new_len <= self.capacity() { let new_len = new_len as u32; // No overflow, because new_len <= capacity <= u32::MAX - // SAFETY: - // - `self.len <= new_len``, because the addition didn't overflow - // - `new_len <= self.capacity()` as per check above + // SAFETY: + // - `self.len <= new_len``, because the addition didn't overflow + // - `new_len <= self.capacity()` as per check above let target = unsafe { self.storage.subslice_mut(self.len, new_len) }; // SAFETY: // - `other.as_ptr()` is valid for reads of `other.len()` elements, because it's a valid slice reference diff --git a/src/containers/storage/heap.rs b/src/containers/storage/heap.rs index 332c1129..e6acd679 100644 --- a/src/containers/storage/heap.rs +++ b/src/containers/storage/heap.rs @@ -11,9 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 // ******************************************************************************* -use alloc::alloc::Layout; use alloc::alloc::alloc; use alloc::alloc::dealloc; +use alloc::alloc::Layout; use core::marker::PhantomData; use core::mem::MaybeUninit; use core::ptr; @@ -47,7 +47,12 @@ impl Storage for Heap { /// /// Panics if the memory allocation failed. fn new(capacity: u32) -> Self { - Self::try_new(capacity).unwrap_or_else(|| panic!("failed to allocate {capacity} elements of {typ}", typ = core::any::type_name::())) + Self::try_new(capacity).unwrap_or_else(|| { + panic!( + "failed to allocate {capacity} elements of {typ}", + typ = core::any::type_name::() + ) + }) } /// Tries to create a new instance with capacity for exactly the given number of elements. @@ -151,7 +156,10 @@ mod tests { let end_slice = unsafe { instance.subslice(capacity - 1, capacity) }; assert_eq!(end_slice.len(), 1); - assert_eq!(end_slice as *const T, instance.elements.as_ptr().wrapping_add(capacity as usize - 1)); + assert_eq!( + end_slice as *const T, + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) + ); } } @@ -182,7 +190,10 @@ mod tests { let end_slice = unsafe { instance.subslice_mut(capacity - 1, capacity) }; assert_eq!(end_slice.len(), 1); - assert_eq!(end_slice as *mut T, instance.elements.as_ptr().wrapping_add(capacity as usize - 1)); + assert_eq!( + end_slice as *mut T, + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) + ); } } @@ -203,7 +214,10 @@ mod tests { assert_eq!(first_element.as_ptr(), instance.elements.as_ptr()); let last_element = unsafe { instance.element(capacity - 1) }; - assert_eq!(last_element.as_ptr(), instance.elements.as_ptr().wrapping_add(capacity as usize - 1)); + assert_eq!( + last_element.as_ptr(), + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) + ); } if capacity >= 2 { @@ -211,7 +225,10 @@ mod tests { assert_eq!(second_element.as_ptr(), instance.elements.as_ptr().wrapping_add(1)); let last_element = unsafe { instance.element(capacity - 2) }; - assert_eq!(last_element.as_ptr(), instance.elements.as_ptr().wrapping_add(capacity as usize - 2)); + assert_eq!( + last_element.as_ptr(), + instance.elements.as_ptr().wrapping_add(capacity as usize - 2) + ); } } @@ -232,7 +249,10 @@ mod tests { assert_eq!(first_element.as_ptr(), instance.elements.as_ptr()); let last_element = unsafe { instance.element_mut(capacity - 1) }; - assert_eq!(last_element.as_ptr(), instance.elements.as_ptr().wrapping_add(capacity as usize - 1)); + assert_eq!( + last_element.as_ptr(), + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) + ); } if capacity >= 2 { @@ -240,7 +260,10 @@ mod tests { assert_eq!(second_element.as_ptr(), instance.elements.as_ptr().wrapping_add(1)); let last_element = unsafe { instance.element_mut(capacity - 2) }; - assert_eq!(last_element.as_ptr(), instance.elements.as_ptr().wrapping_add(capacity as usize - 2)); + assert_eq!( + last_element.as_ptr(), + instance.elements.as_ptr().wrapping_add(capacity as usize - 2) + ); } } diff --git a/src/containers/storage/inline.rs b/src/containers/storage/inline.rs index b6555426..fc65da46 100644 --- a/src/containers/storage/inline.rs +++ b/src/containers/storage/inline.rs @@ -123,17 +123,11 @@ mod tests { let empty_slice = unsafe { instance.subslice(0, 0) }; assert_eq!(empty_slice.len(), 0); - assert_eq!( - empty_slice as *const T, - instance.elements.as_ptr() as *const T - ); + assert_eq!(empty_slice as *const T, instance.elements.as_ptr() as *const T); let full_slice = unsafe { instance.subslice(0, capacity) }; assert_eq!(full_slice.len(), capacity as usize); - assert_eq!( - full_slice as *const T, - instance.elements.as_ptr() as *const T - ); + assert_eq!(full_slice as *const T, instance.elements.as_ptr() as *const T); if capacity > 2 { let partial_slice = unsafe { instance.subslice(1, 2) }; @@ -147,10 +141,7 @@ mod tests { assert_eq!(end_slice.len(), 1); assert_eq!( end_slice as *const T, - instance - .elements - .as_ptr() - .wrapping_add(capacity as usize - 1) as *const T + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) as *const T ); } } @@ -190,10 +181,7 @@ mod tests { assert_eq!(end_slice.len(), 1); assert_eq!( end_slice as *mut T, - instance - .elements - .as_ptr() - .wrapping_add(capacity as usize - 1) as *mut T + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) as *mut T ); } } @@ -215,18 +203,12 @@ mod tests { if capacity >= 1 { let first_element = unsafe { instance.element(0) }; - assert_eq!( - first_element.as_ptr(), - instance.elements.as_ptr() as *const T - ); + assert_eq!(first_element.as_ptr(), instance.elements.as_ptr() as *const T); let last_element = unsafe { instance.element(capacity - 1) }; assert_eq!( last_element.as_ptr(), - instance - .elements - .as_ptr() - .wrapping_add(capacity as usize - 1) as *const T, + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) as *const T, ); } @@ -240,10 +222,7 @@ mod tests { let last_element = unsafe { instance.element(capacity - 2) }; assert_eq!( last_element.as_ptr(), - instance - .elements - .as_ptr() - .wrapping_add(capacity as usize - 2) as *const T, + instance.elements.as_ptr().wrapping_add(capacity as usize - 2) as *const T, ); } } @@ -270,10 +249,7 @@ mod tests { let last_element = unsafe { instance.element_mut(capacity - 1) }; assert_eq!( last_element.as_ptr(), - instance - .elements - .as_ptr() - .wrapping_add(capacity as usize - 1) as *mut T, + instance.elements.as_ptr().wrapping_add(capacity as usize - 1) as *mut T, ); } @@ -287,10 +263,7 @@ mod tests { let last_element = unsafe { instance.element_mut(capacity - 2) }; assert_eq!( last_element.as_ptr(), - instance - .elements - .as_ptr() - .wrapping_add(capacity as usize - 2) as *mut T, + instance.elements.as_ptr().wrapping_add(capacity as usize - 2) as *mut T, ); } } diff --git a/src/log/mw_log_fmt/builders.rs b/src/log/mw_log_fmt/builders.rs index 66f1bf8f..04585d76 100644 --- a/src/log/mw_log_fmt/builders.rs +++ b/src/log/mw_log_fmt/builders.rs @@ -253,7 +253,10 @@ impl<'a> DebugSet<'a> { /// Finishes output and returns any error encountered. pub fn finish(&mut self) -> Result { - self.inner.result = self.inner.result.and_then(|_| self.inner.writer.write_str("}", &FormatSpec::new())); + self.inner.result = self + .inner + .result + .and_then(|_| self.inner.writer.write_str("}", &FormatSpec::new())); self.inner.result } } @@ -323,7 +326,10 @@ impl<'a> DebugList<'a> { /// Finishes output and returns any error encountered. pub fn finish(&mut self) -> Result { - self.inner.result = self.inner.result.and_then(|_| self.inner.writer.write_str("]", &FormatSpec::new())); + self.inner.result = self + .inner + .result + .and_then(|_| self.inner.writer.write_str("]", &FormatSpec::new())); self.inner.result } } @@ -542,7 +548,9 @@ mod tests { let mut writer = StringWriter::new(); let spec = FormatSpec::new(); - let _ = DebugStruct::new(&mut writer, &spec, "X").finish().map_err(|_| panic!("failed to finish")); + let _ = DebugStruct::new(&mut writer, &spec, "X") + .finish() + .map_err(|_| panic!("failed to finish")); assert_eq!(writer.get(), format!("{:?}", v)); } @@ -594,7 +602,9 @@ mod tests { fn test_tuple_empty_finish() { let mut writer = StringWriter::new(); let spec = FormatSpec::new(); - let _ = DebugTuple::new(&mut writer, &spec, "").finish().map_err(|_| panic!("failed to finish")); + let _ = DebugTuple::new(&mut writer, &spec, "") + .finish() + .map_err(|_| panic!("failed to finish")); assert_eq!(writer.get(), ""); } diff --git a/src/log/mw_log_fmt/fmt.rs b/src/log/mw_log_fmt/fmt.rs index 60f3a395..05ff8d0c 100644 --- a/src/log/mw_log_fmt/fmt.rs +++ b/src/log/mw_log_fmt/fmt.rs @@ -164,7 +164,9 @@ mod tests { let result = ScoreDebug::fmt(&args, &mut w, &FormatSpec::new()); assert!(result == Ok(())); - assert!(w.get() == "test_true123.4432.2-100-1234-123456-120000000000000000012312341234561200000000000000000_string") + assert!( + w.get() == "test_true123.4432.2-100-1234-123456-120000000000000000012312341234561200000000000000000_string" + ) } #[test] diff --git a/src/log/mw_log_fmt/fmt_impl_qm.rs b/src/log/mw_log_fmt/fmt_impl_qm.rs index 3ef12bf3..98e459de 100644 --- a/src/log/mw_log_fmt/fmt_impl_qm.rs +++ b/src/log/mw_log_fmt/fmt_impl_qm.rs @@ -35,7 +35,10 @@ impl ScoreDebug for Path { } f.write_str(valid, spec)?; - f.write_str(core::char::REPLACEMENT_CHARACTER.encode_utf8(&mut [0; MAX_LEN_UTF8]), spec)?; + f.write_str( + core::char::REPLACEMENT_CHARACTER.encode_utf8(&mut [0; MAX_LEN_UTF8]), + spec, + )?; } Ok(()) diff --git a/src/log/mw_log_fmt/fmt_spec.rs b/src/log/mw_log_fmt/fmt_spec.rs index 54beb132..881178ed 100644 --- a/src/log/mw_log_fmt/fmt_spec.rs +++ b/src/log/mw_log_fmt/fmt_spec.rs @@ -291,7 +291,17 @@ mod tests { let width = Some(1234); let precision = Some(5); - let format_spec = FormatSpec::from_params(display_hint, fill, align, sign, alternate, zero_pad, debug_as_hex, width, precision); + let format_spec = FormatSpec::from_params( + display_hint, + fill, + align, + sign, + alternate, + zero_pad, + debug_as_hex, + width, + precision, + ); assert!(format_spec.get_display_hint() == display_hint); assert!(format_spec.get_fill() == fill);