Skip to content

Commit

Permalink
chore: Update to Rust 2024 edition
Browse files Browse the repository at this point in the history
That includes the following changes in the standard:

* `unsafe` calls need to be included in `unsafe` blocks explicitly,
  even inside `unsafe` functions, otherwise triggering a warning. That
  helps differentiating between `unsafe` calls (which can be safe, if
  we can guarantee that ourselves) and `unsafe` functions (which don't
  guarantee safety by themselves).
* Unnecessary `ref` or `mut` keywords in match statements are compiler
  errors.
  • Loading branch information
vadorovsky committed Mar 10, 2025
1 parent 4ef97d2 commit 73f0233
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ members = [
[workspace.package]
version = "0.9.0"
license = "Apache-2.0 WITH BPF probes exception under GPL-2.0"
edition = "2021"
edition = "2024"
repository = "https://github.com/exein-io/pulsar"

[workspace.dependencies]
Expand Down
27 changes: 14 additions & 13 deletions crates/bpf-common/src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ impl ContainerInfo {
}

fn from_libpod_id(id: String, uid: Uid) -> Result<Self, ContainerError> {
let user_home =
unsafe { get_user_home_dir(uid) }.ok_or(ContainerError::HomeDirNotFound { uid })?;
let user_home = get_user_home_dir(uid).ok_or(ContainerError::HomeDirNotFound { uid })?;

let user_home = Path::new(&user_home);

Expand Down Expand Up @@ -404,24 +403,26 @@ impl LibpodDatabaseBackend {
}
}

unsafe fn get_user_home_dir(uid: Uid) -> Option<OsString> {
let amt = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
fn get_user_home_dir(uid: Uid) -> Option<OsString> {
let amt = match unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) } {
n if n < 0 => 512_usize,
n => n as usize,
};
let mut buf = Vec::with_capacity(amt);
let mut passwd: libc::passwd = mem::zeroed();
let mut passwd: libc::passwd = unsafe { mem::zeroed() };
let mut result = ptr::null_mut();
match libc::getpwuid_r(
uid.as_raw(),
&mut passwd,
buf.as_mut_ptr(),
buf.capacity(),
&mut result,
) {
match unsafe {
libc::getpwuid_r(
uid.as_raw(),
&mut passwd,
buf.as_mut_ptr(),
buf.capacity(),
&mut result,
)
} {
0 if !result.is_null() => {
let ptr = passwd.pw_dir as *const _;
let bytes = CStr::from_ptr(ptr).to_bytes();
let bytes = unsafe { CStr::from_ptr(ptr).to_bytes() };
if bytes.is_empty() {
None
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/modules/threat-logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl ThreatLogger {
println!("{out}");
}

if let Some(ref mut syslog) = &mut self.syslog {
if let Some(syslog) = &mut self.syslog {
let out = match self.output_format {
OutputFormat::Plaintext => format!("<{PRIORITY}>{event}"),
OutputFormat::Json => format!("<{PRIORITY}>{}", json_event()?),
Expand Down
10 changes: 6 additions & 4 deletions crates/validatron/src/reflection/adt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
any::{type_name, Any},
any::{Any, type_name},
collections::HashMap,
marker::PhantomData,
};
Expand Down Expand Up @@ -99,7 +99,9 @@ fn add_variant(
.insert(field_name, variant)
.is_some()
{
panic!("you added two variant+field with the same name '{variant_name}+{field_name}' into a enum class definition")
panic!(
"you added two variant+field with the same name '{variant_name}+{field_name}' into a enum class definition"
)
}
}

Expand Down Expand Up @@ -169,7 +171,7 @@ impl VariantAttribute {
/// The `unsafe` is related to the returned function. That function accepts values as [Any],
/// but must be called with values of the right type, because it doesn't perform checks.
pub unsafe fn into_extractor_fn_unchecked(self) -> UncheckedDynEnumFieldExtractorFn {
self.inner.into_extractor_fn_unchecked()
unsafe { self.inner.into_extractor_fn_unchecked() }
}
}

Expand Down Expand Up @@ -208,7 +210,7 @@ where

unsafe fn into_extractor_fn_unchecked(self: Box<Self>) -> UncheckedDynEnumFieldExtractorFn {
Box::new(move |source| {
let source = &*(source as *const dyn Any as *const T);
let source = unsafe { &*(source as *const dyn Any as *const T) };

(self.extractor)(source).map(|res| res as _)
})
Expand Down
30 changes: 17 additions & 13 deletions crates/validatron/src/reflection/collection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
any::{type_name, Any},
any::{Any, type_name},
marker::PhantomData,
};

Expand Down Expand Up @@ -81,7 +81,7 @@ impl Collection {
&self,
value: &str,
) -> Result<DynContainsFnUnchecked, ValidatronError> {
self.inner.contains_fn_any_value_unchecked(value)
unsafe { self.inner.contains_fn_any_value_unchecked(value) }
}

pub fn contains_fn_any_multi(&self) -> Result<DynContainsMulti, ValidatronError> {
Expand All @@ -95,7 +95,7 @@ impl Collection {
pub unsafe fn contains_fn_any_multi_unchecked(
&self,
) -> Result<DynContainsMultiUnchecked, ValidatronError> {
self.inner.contains_fn_any_multi_unchecked()
unsafe { self.inner.contains_fn_any_multi_unchecked() }
}
}

Expand Down Expand Up @@ -166,13 +166,15 @@ where
return Err(ValidatronError::CollectionValueNotPrimitive);
};

let cmp = primitive.compare_fn_any_value_unchecked(
crate::Operator::Relational(crate::RelationalOperator::Equals),
value,
)?;
let cmp = unsafe {
primitive.compare_fn_any_value_unchecked(
crate::Operator::Relational(crate::RelationalOperator::Equals),
value,
)?
};

Ok(Box::new(move |source| {
let source = &*(source as *const dyn Any as *const T);
let source = unsafe { &*(source as *const dyn Any as *const T) };

source.into_iter().any(|item| cmp(item))
}))
Expand Down Expand Up @@ -208,13 +210,15 @@ where
return Err(ValidatronError::CollectionValueNotPrimitive);
};

let cmp = primitive.compare_fn_any_multi_unchecked(crate::Operator::Relational(
crate::RelationalOperator::Equals,
))?;
let cmp = unsafe {
primitive.compare_fn_any_multi_unchecked(crate::Operator::Relational(
crate::RelationalOperator::Equals,
))?
};

Ok(Box::new(move |collection, second| {
let collection = &*(collection as *const dyn Any as *const T);
let second = &*(second as *const dyn Any as *const U);
let collection = unsafe { &*(collection as *const dyn Any as *const T) };
let second = unsafe { &*(second as *const dyn Any as *const U) };

collection.into_iter().any(|item| cmp(item, second))
}))
Expand Down
6 changes: 3 additions & 3 deletions crates/validatron/src/reflection/methods.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
any::{type_name, Any},
any::{Any, type_name},
collections::HashMap,
marker::PhantomData,
};
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Method {
/// The `unsafe` is related to the returned function. That function accepts values as [Any],
/// but must be called with values of the right type, because it doesn't perform checks.
pub unsafe fn into_extractor_fn_unchecked(self) -> UncheckedDynMethod0CallFn {
self.inner.into_extractor_fn_unchecked()
unsafe { self.inner.into_extractor_fn_unchecked() }
}
}

Expand Down Expand Up @@ -164,7 +164,7 @@ where

unsafe fn into_extractor_fn_unchecked(self: Box<Self>) -> UncheckedDynMethod0CallFn {
Box::new(move |source| {
let source = &*(source as *const dyn Any as *const T);
let source = unsafe { &*(source as *const dyn Any as *const T) };

Box::new((self.executor)(source)) as _
})
Expand Down
12 changes: 6 additions & 6 deletions crates/validatron/src/reflection/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::any::{type_name, Any, TypeId};
use std::any::{Any, TypeId, type_name};

use crate::{
HandleOperatorFn, MethodsBuilder, Operator, Validatron, ValidatronClass, ValidatronClassKind,
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Primitive {
op: Operator,
value: &str,
) -> Result<DynOperatorConstUnchecked, ValidatronError> {
self.inner.compare_fn_any_value_unchecked(op, value)
unsafe { self.inner.compare_fn_any_value_unchecked(op, value) }
}

pub fn compare_fn_any_multi(&self, op: Operator) -> Result<DynOperatorMulti, ValidatronError> {
Expand All @@ -105,7 +105,7 @@ impl Primitive {
&self,
op: Operator,
) -> Result<DynOperatorMultiUnchecked, ValidatronError> {
self.inner.compare_fn_any_multi_unchecked(op)
unsafe { self.inner.compare_fn_any_multi_unchecked(op) }
}

pub fn field_type_id(&self) -> TypeId {
Expand Down Expand Up @@ -171,7 +171,7 @@ where
let compare_fn = (self.handle_op_fn)(op)?;

Ok(Box::new(move |source| {
let source = &*(source as *const dyn Any as *const T);
let source = unsafe { &*(source as *const dyn Any as *const T) };
compare_fn(source, &value)
}))
}
Expand All @@ -197,8 +197,8 @@ where
let compare_fn = (self.handle_op_fn)(op)?;

Ok(Box::new(move |first, second| {
let first = &*(first as *const dyn Any as *const T);
let second = &*(second as *const dyn Any as *const T);
let first = unsafe { &*(first as *const dyn Any as *const T) };
let second = unsafe { &*(second as *const dyn Any as *const T) };

compare_fn(first, second)
}))
Expand Down
6 changes: 3 additions & 3 deletions crates/validatron/src/reflection/structure.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
any::{type_name, Any},
any::{Any, type_name},
collections::HashMap,
marker::PhantomData,
};
Expand Down Expand Up @@ -143,7 +143,7 @@ impl Attribute {
/// The `unsafe` is related to the returned function. That function accepts values as [Any],
/// but must be called with values of the right type, because it doesn't perform checks.
pub unsafe fn into_extractor_fn_unchecked(self) -> UncheckedDynFieldExtractorFn {
self.inner.into_extractor_fn_unchecked()
unsafe { self.inner.into_extractor_fn_unchecked() }
}
}

Expand Down Expand Up @@ -182,7 +182,7 @@ where

unsafe fn into_extractor_fn_unchecked(self: Box<Self>) -> UncheckedDynFieldExtractorFn {
Box::new(move |source| {
let source = &*(source as *const dyn Any as *const T);
let source = unsafe { &*(source as *const dyn Any as *const T) };

(self.extractor)(source)
})
Expand Down
2 changes: 1 addition & 1 deletion src/pulsard/module_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<T: PulsarModule> ModuleManager<T> {
}

pub fn add_warning(&mut self, warning: String) {
if let ModuleStatus::Running(ref mut warnings) = &mut self.status {
if let ModuleStatus::Running(warnings) = &mut self.status {
warnings.push(warning);
}
}
Expand Down

0 comments on commit 73f0233

Please sign in to comment.