Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/rustfmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

bazel run @score_tooling//format_checker:rustfmt_with_policies
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
)

Expand Down
12 changes: 4 additions & 8 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion src/containers/fixed_capacity/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion src/containers/generic/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, S: Storage<T>> {
Expand Down
2 changes: 1 addition & 1 deletion src/containers/generic/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 7 additions & 5 deletions src/containers/generic/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, S: Storage<T>> {
Expand Down Expand Up @@ -143,12 +143,14 @@ impl<T: Copy, S: Storage<T>> GenericVec<T, S> {
/// 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
Expand Down
39 changes: 31 additions & 8 deletions src/containers/storage/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +47,12 @@ impl<T> Storage<T> for Heap<T> {
///
/// 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::<T>()))
Self::try_new(capacity).unwrap_or_else(|| {
panic!(
"failed to allocate {capacity} elements of {typ}",
typ = core::any::type_name::<T>()
)
})
}

/// Tries to create a new instance with capacity for exactly the given number of elements.
Expand Down Expand Up @@ -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)
);
}
}

Expand Down Expand Up @@ -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)
);
}
}

Expand All @@ -203,15 +214,21 @@ 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 {
let second_element = unsafe { instance.element(1) };
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)
);
}
}

Expand All @@ -232,15 +249,21 @@ 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 {
let second_element = unsafe { instance.element_mut(1) };
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)
);
}
}

Expand Down
45 changes: 9 additions & 36 deletions src/containers/storage/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
Expand All @@ -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
);
}
}
Expand Down Expand Up @@ -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
);
}
}
Expand All @@ -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,
);
}

Expand All @@ -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,
);
}
}
Expand All @@ -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,
);
}

Expand All @@ -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,
);
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/log/mw_log_fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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(), "");
}
Expand Down
4 changes: 3 additions & 1 deletion src/log/mw_log_fmt/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 4 additions & 1 deletion src/log/mw_log_fmt/fmt_impl_qm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
12 changes: 11 additions & 1 deletion src/log/mw_log_fmt/fmt_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading