Skip to content

Commit

Permalink
Merge pull request #581 from elBoberido/iox-579-use-core-instead-of-std
Browse files Browse the repository at this point in the history
[#579] [#581] Use 'core' and 'alloc' instead of 'std'
  • Loading branch information
elBoberido authored Jan 13, 2025
2 parents df775df + 775bd31 commit 57c5b16
Show file tree
Hide file tree
Showing 277 changed files with 1,062 additions and 915 deletions.
2 changes: 1 addition & 1 deletion benchmarks/event/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn perform_benchmark<T: Service>(args: &Args) {
let stop = start.elapsed().expect("failed to measure time");
println!(
"{} ::: MaxEventId: {}, Iterations: {}, Time: {} s, Latency: {} ns",
std::any::type_name::<T>(),
core::any::type_name::<T>(),
args.max_event_id,
args.iterations,
stop.as_secs_f64(),
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/publish-subscribe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::mem::MaybeUninit;
use core::mem::MaybeUninit;

use clap::Parser;
use iceoryx2::prelude::*;
Expand Down Expand Up @@ -138,7 +138,7 @@ fn perform_benchmark<T: Service>(args: &Args) -> Result<(), Box<dyn std::error::
let stop = start.elapsed().expect("failed to measure time");
println!(
"{} ::: Iterations: {}, Time: {} s, Latency: {} ns, Sample Size: {}",
std::any::type_name::<T>(),
core::any::type_name::<T>(),
args.iterations,
stop.as_secs_f64(),
stop.as_nanos() / (args.iterations as u128 * 2),
Expand Down
24 changes: 12 additions & 12 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
disallowed-types = [
"std::sync::atomic::AtomicI8",
"std::sync::atomic::AtomicI16",
"std::sync::atomic::AtomicI32",
"std::sync::atomic::AtomicI64",
"std::sync::atomic::AtomicU8",
"std::sync::atomic::AtomicU16",
"std::sync::atomic::AtomicU32",
"std::sync::atomic::AtomicU64",
"std::sync::atomic::AtomicBool",
"std::sync::atomic::AtomicIsize",
"std::sync::atomic::AtomicPtr",
"std::sync::atomic::AtomicUsize",
"core::sync::atomic::AtomicI8",
"core::sync::atomic::AtomicI16",
"core::sync::atomic::AtomicI32",
"core::sync::atomic::AtomicI64",
"core::sync::atomic::AtomicU8",
"core::sync::atomic::AtomicU16",
"core::sync::atomic::AtomicU32",
"core::sync::atomic::AtomicU64",
"core::sync::atomic::AtomicBool",
"core::sync::atomic::AtomicIsize",
"core::sync::atomic::AtomicPtr",
"core::sync::atomic::AtomicUsize",
"core::sync::atomic::AtomicI8",
"core::sync::atomic::AtomicI16",
"core::sync::atomic::AtomicI32",
Expand Down
4 changes: 4 additions & 0 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

* Deadline property for event services
[#573](https://github.com/eclipse-iceoryx/iceoryx2/issues/573)
* Use 'std_instead_of_core' clippy warning
[#579](https://github.com/eclipse-iceoryx/iceoryx2/issues/579)
* Use 'std_instead_of_alloc' and 'alloc_instead_of_core' clippy warning
[#581](https://github.com/eclipse-iceoryx/iceoryx2/issues/581)

### Bugfixes

Expand Down
2 changes: 1 addition & 1 deletion examples/rust/complex_data_types/complex_data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::time::Duration;
use core::time::Duration;

use iceoryx2::prelude::*;
use iceoryx2_bb_container::{
Expand Down
2 changes: 1 addition & 1 deletion examples/rust/event_based_communication/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use core::time::Duration;
use examples_common::{PubSubEvent, TransmissionData};
use iceoryx2::{
port::{
Expand All @@ -18,7 +19,6 @@ use iceoryx2::{
},
prelude::*,
};
use std::time::Duration;

const CYCLE_TIME: Duration = Duration::from_secs(1);
const HISTORY_SIZE: usize = 20;
Expand Down
2 changes: 1 addition & 1 deletion examples/rust/event_based_communication/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::time::Duration;
use core::time::Duration;

use examples_common::{PubSubEvent, TransmissionData};
use iceoryx2::{
Expand Down
28 changes: 14 additions & 14 deletions iceoryx2-bb/container/src/byte_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//! println!("removed byte {}", some_string.remove(0));
//! ```
use std::{
use core::{
cmp::Ordering,
fmt::{Debug, Display},
hash::Hash,
Expand Down Expand Up @@ -67,8 +67,8 @@ pub enum FixedSizeByteStringModificationError {
InsertWouldExceedCapacity,
}

impl std::fmt::Display for FixedSizeByteStringModificationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Display for FixedSizeByteStringModificationError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
std::write!(f, "FixedSizeByteStringModificationError::{:?}", self)
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ struct FixedSizeByteStringVisitor<const CAPACITY: usize>;
impl<const CAPACITY: usize> Visitor<'_> for FixedSizeByteStringVisitor<CAPACITY> {
type Value = FixedSizeByteString<CAPACITY>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str(&format!("a string with a length of at most {}", CAPACITY))
}

Expand Down Expand Up @@ -147,7 +147,7 @@ impl<const CAPACITY: usize> Ord for FixedSizeByteString<CAPACITY> {
}

impl<const CAPACITY: usize> Hash for FixedSizeByteString<CAPACITY> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
state.write(self.as_bytes())
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl<const CAPACITY: usize, const OTHER_CAPACITY: usize> PartialEq<&[u8; OTHER_C
}

impl<const CAPACITY: usize> Debug for FixedSizeByteString<CAPACITY> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"FixedSizeByteString<{}> {{ len: {}, data: \"{}\" }}",
Expand All @@ -211,7 +211,7 @@ impl<const CAPACITY: usize> Debug for FixedSizeByteString<CAPACITY> {
}

impl<const CAPACITY: usize> Display for FixedSizeByteString<CAPACITY> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", as_escaped_string(self.as_bytes()))
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {
/// into the [`FixedSizeByteString`] it will be truncated.
pub fn from_bytes_truncated(bytes: &[u8]) -> Self {
let mut new_self = Self::new();
new_self.len = std::cmp::min(bytes.len(), CAPACITY);
new_self.len = core::cmp::min(bytes.len(), CAPACITY);
for (i, byte) in bytes.iter().enumerate().take(new_self.len) {
new_self.data[i].write(*byte);
}
Expand Down Expand Up @@ -332,7 +332,7 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {
}

let mut new_self = Self::new();
std::ptr::copy_nonoverlapping(
core::ptr::copy_nonoverlapping(
ptr,
new_self.as_mut_bytes().as_mut_ptr() as *mut core::ffi::c_char,
string_length,
Expand All @@ -344,12 +344,12 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {

/// Returns a slice to the underlying bytes
pub const fn as_bytes(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.data[0].as_ptr(), self.len) }
unsafe { core::slice::from_raw_parts(self.data[0].as_ptr(), self.len) }
}

/// Returns a null-terminated slice to the underlying bytes
pub const fn as_bytes_with_nul(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.data[0].as_ptr(), self.len + 1) }
unsafe { core::slice::from_raw_parts(self.data[0].as_ptr(), self.len + 1) }
}

/// Returns a zero terminated slice of the underlying bytes
Expand All @@ -359,7 +359,7 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {

/// Returns a mutable slice to the underlying bytes
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
unsafe { std::slice::from_raw_parts_mut(self.data[0].as_mut_ptr(), self.len) }
unsafe { core::slice::from_raw_parts_mut(self.data[0].as_mut_ptr(), self.len) }
}

/// Returns the capacity of the string
Expand Down Expand Up @@ -452,7 +452,7 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {
///
pub unsafe fn insert_bytes_unchecked(&mut self, idx: usize, bytes: &[u8]) {
unsafe {
std::ptr::copy(
core::ptr::copy(
self.data[idx].as_ptr(),
self.data[idx].as_mut_ptr().add(bytes.len()),
self.len - idx,
Expand Down Expand Up @@ -528,7 +528,7 @@ impl<const CAPACITY: usize> FixedSizeByteString<CAPACITY> {

fn remove_range_impl(&mut self, idx: usize, len: usize) {
unsafe {
std::ptr::copy(
core::ptr::copy(
self.data[idx + len].as_ptr(),
self.data[idx].as_mut_ptr(),
self.len - (idx + len),
Expand Down
3 changes: 3 additions & 0 deletions iceoryx2-bb/container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![warn(clippy::alloc_instead_of_core)]
#![warn(clippy::std_instead_of_alloc)]
#![warn(clippy::std_instead_of_core)]
#![warn(missing_docs)]

//! # iceoryx2 Building Blocks (BB) Container
Expand Down
22 changes: 11 additions & 11 deletions iceoryx2-bb/container/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
//! use iceoryx2_bb_container::queue::RelocatableQueue;
//! use iceoryx2_bb_elementary::bump_allocator::BumpAllocator;
//! use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer;
//! use std::ptr::NonNull;
//! use core::ptr::NonNull;
//!
//! const QUEUE_CAPACITY:usize = 12;
//! const MEM_SIZE: usize = RelocatableQueue::<u128>::const_memory_size(QUEUE_CAPACITY);
Expand All @@ -101,6 +101,8 @@
//! unsafe { queue.init(&bump_allocator).expect("queue init failed") };
//! ```
//!
use core::marker::PhantomData;
use core::{alloc::Layout, fmt::Debug, mem::MaybeUninit};
use iceoryx2_bb_elementary::allocator::{AllocationError, BaseAllocator};
use iceoryx2_bb_elementary::bump_allocator::BumpAllocator;
use iceoryx2_bb_elementary::math::unaligned_mem_size;
Expand All @@ -111,8 +113,6 @@ pub use iceoryx2_bb_elementary::relocatable_container::RelocatableContainer;
use iceoryx2_bb_elementary::relocatable_ptr::{GenericRelocatablePointer, RelocatablePointer};
use iceoryx2_bb_log::{fail, fatal_panic};
use iceoryx2_pal_concurrency_sync::iox_atomic::IoxAtomicBool;
use std::marker::PhantomData;
use std::{alloc::Layout, fmt::Debug, mem::MaybeUninit};

/// Queue with run-time fixed size capacity. In contrast to its counterpart the
/// [`RelocatableQueue`] it is movable but is not shared memory compatible.
Expand Down Expand Up @@ -232,7 +232,7 @@ pub mod details {
) -> Result<(), AllocationError> {
if self
.is_initialized
.load(std::sync::atomic::Ordering::Relaxed)
.load(core::sync::atomic::Ordering::Relaxed)
{
fatal_panic!(
from "Queue::init()",
Expand All @@ -242,12 +242,12 @@ pub mod details {

self.data_ptr.init(fail!(from "Queue::init", when allocator
.allocate(Layout::from_size_align_unchecked(
std::mem::size_of::<T>() * self.capacity,
std::mem::align_of::<T>(),
core::mem::size_of::<T>() * self.capacity,
core::mem::align_of::<T>(),
)), "Failed to initialize queue since the allocation of the data memory failed."
));
self.is_initialized
.store(true, std::sync::atomic::Ordering::Relaxed);
.store(true, core::sync::atomic::Ordering::Relaxed);

Ok(())
}
Expand Down Expand Up @@ -332,9 +332,9 @@ pub mod details {
fn verify_init(&self, source: &str) {
debug_assert!(
self.is_initialized
.load(std::sync::atomic::Ordering::Relaxed),
.load(core::sync::atomic::Ordering::Relaxed),
"From: MetaQueue<{}>::{}, Undefined behavior - the object was not initialized with 'init' before.",
std::any::type_name::<T>(), source
core::any::type_name::<T>(), source
);
}

Expand Down Expand Up @@ -395,7 +395,7 @@ pub mod details {

let index = (self.start - self.len) % self.capacity;
self.len -= 1;
let value = std::mem::replace(
let value = core::mem::replace(
&mut *self.data_ptr.as_mut_ptr().add(index),
MaybeUninit::uninit(),
);
Expand Down Expand Up @@ -441,7 +441,7 @@ pub mod details {
fn drop(&mut self) {
if self
.is_initialized
.load(std::sync::atomic::Ordering::Relaxed)
.load(core::sync::atomic::Ordering::Relaxed)
{
unsafe { self.clear_impl() }
}
Expand Down
Loading

0 comments on commit 57c5b16

Please sign in to comment.