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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ jobs:
- name: "Run Analyzer"
run: cargo check ${CI_CARGO_ARGS}

- name: "Run Clippy"
run: cargo clippy ${CI_CARGO_ARGS}

#
# A complete but basic build of the project, running on common x86-64 ubuntu
# machines. This should catch most compilation errors or test-suite failures
Expand Down
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ osi = { path = "./lib/osi", version = "1.0.0" }
sys = { path = "./lib/sys", version = "1.0.0" }
tmp = { path = "./lib/tmp", version = "1.0.0" }

[workspace.lints.clippy]
assertions_on_constants = "allow"
explicit_auto_deref = "allow"
identity_op = "allow"
inherent_to_string_shadow_display = "allow"
len_zero = "allow"
manual_range_contains = "allow"
needless_late_init = "allow"
new_without_default = "allow"
should_implement_trait = "allow"
redundant_field_names = "allow"

[workspace.lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
Expand Down
6 changes: 0 additions & 6 deletions lib/osi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
// that these can be greatly optimized by the compiler, they seem a worthy
// fit for the standard library.

#![allow(clippy::assertions_on_constants)]
#![allow(clippy::identity_op)]
#![allow(clippy::manual_range_contains)]
#![allow(clippy::should_implement_trait)]
#![allow(clippy::redundant_field_names)]

#![no_std]

extern crate alloc;
Expand Down
39 changes: 20 additions & 19 deletions lib/tmp/src/fmt/dbus/dvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,31 @@ impl Format {
}
}

fn u8(&self, v: u8) -> [u8; 1] {
fn en_u8(&self, v: u8) -> [u8; 1] {
[v]
}

fn u16(&self, v: u16) -> [u8; 2] {
fn en_u16(&self, v: u16) -> [u8; 2] {
if self.is_be() { v.to_be_bytes() } else { v.to_le_bytes() }
}

fn u32(&self, v: u32) -> [u8; 4] {
fn en_u32(&self, v: u32) -> [u8; 4] {
if self.is_be() { v.to_be_bytes() } else { v.to_le_bytes() }
}

fn u64(&self, v: u64) -> [u8; 8] {
fn en_u64(&self, v: u64) -> [u8; 8] {
if self.is_be() { v.to_be_bytes() } else { v.to_le_bytes() }
}

fn to_u8(&self, v: [u8; 1]) -> u8 {
fn de_u8(&self, v: [u8; 1]) -> u8 {
if self.is_be() { u8::from_be_bytes(v) } else { u8::from_le_bytes(v) }
}

fn to_u16(&self, v: [u8; 2]) -> u16 {
fn de_u16(&self, v: [u8; 2]) -> u16 {
if self.is_be() { u16::from_be_bytes(v) } else { u16::from_le_bytes(v) }
}

fn to_u32(&self, v: [u8; 4]) -> u32 {
fn de_u32(&self, v: [u8; 4]) -> u32 {
if self.is_be() { u32::from_be_bytes(v) } else { u32::from_le_bytes(v) }
}
}
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<'sig, 'write> Enc<'sig, 'write> {

let mut idx = self.level.idx;
Self::align(self.write, &mut idx, dbus::Element::U8.dvar_alignment_exp())?;
Self::write(self.write, &mut idx, &self.format.u8(n))?;
Self::write(self.write, &mut idx, &self.format.en_u8(n))?;
Self::write(self.write, &mut idx, data.as_bytes())?;
Self::zero(self.write, &mut idx, 1)?;
self.level.idx = idx;
Expand All @@ -279,7 +279,7 @@ impl<'sig, 'write> Enc<'sig, 'write> {

let mut idx = self.level.idx;
Self::align(self.write, &mut idx, dbus::Element::U32.dvar_alignment_exp())?;
Self::write(self.write, &mut idx, &self.format.u32(n))?;
Self::write(self.write, &mut idx, &self.format.en_u32(n))?;
Self::write(self.write, &mut idx, data.as_bytes())?;
Self::zero(self.write, &mut idx, 1)?;
self.level.idx = idx;
Expand All @@ -289,15 +289,15 @@ impl<'sig, 'write> Enc<'sig, 'write> {
}

pub fn u16(&mut self, data: u16) -> Flow<Option<dbus::Error>, &mut Self> {
self.fixed(dbus::Element::U16, &self.format.u16(data))
self.fixed(dbus::Element::U16, &self.format.en_u16(data))
}

pub fn u32(&mut self, data: u32) -> Flow<Option<dbus::Error>, &mut Self> {
self.fixed(dbus::Element::U32, &self.format.u32(data))
self.fixed(dbus::Element::U32, &self.format.en_u32(data))
}

pub fn u64(&mut self, data: u64) -> Flow<Option<dbus::Error>, &mut Self> {
self.fixed(dbus::Element::U64, &self.format.u64(data))
self.fixed(dbus::Element::U64, &self.format.en_u64(data))
}

pub fn string(&mut self, data: &str) -> Flow<Option<dbus::Error>, &mut Self> {
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<'sig, 'write> Enc<'sig, 'write> {

let mut level = self.level.clone();
Self::align(self.write, &mut level.idx, dbus::Element::U8.dvar_alignment_exp())?;
Self::write(self.write, &mut level.idx, &self.format.u8(n))?;
Self::write(self.write, &mut level.idx, &self.format.en_u8(n))?;
Self::write_iter(self.write, &mut level.idx, &mut sig.into_iter().map(|v| v.code()))?;
Self::zero(self.write, &mut level.idx, 1)?;
level.meta = level.idx;
Expand All @@ -352,7 +352,7 @@ impl<'sig, 'write> Enc<'sig, 'write> {
let align = self.cursor.down().unwrap().dvar_alignment_exp();
Self::align(self.write, &mut level.idx, dbus::Element::U32.dvar_alignment_exp())?;
level.meta = level.idx;
Self::write(self.write, &mut level.idx, &self.format.u32(0))?;
Self::write(self.write, &mut level.idx, &self.format.en_u32(0))?;
Self::align(self.write, &mut level.idx, align)?;
level.from = level.idx;

Expand Down Expand Up @@ -422,7 +422,7 @@ impl<'sig, 'write> Enc<'sig, 'write> {
return Flow::Break(Some(dbus::Error::DataOverflow));
};
let mut idx = self.level.meta;
Self::write(self.write, &mut idx, &self.format.u32(n))?;
Self::write(self.write, &mut idx, &self.format.en_u32(n))?;
}
},
dbus::Element::StructOpen => {
Expand Down Expand Up @@ -598,7 +598,7 @@ impl<'sig, 'read> Dec<'sig, 'read> {
let mut len_u = [0; _];
Self::align(self.read, &mut idx, dbus::Element::U8.dvar_alignment_exp())?;
Self::read(self.read, &mut idx, &mut len_u)?;
let len = self.format.to_u8(len_u) as usize;
let len = self.format.de_u8(len_u) as usize;

// Read the string.
let mut buffer = alloc::vec::Vec::with_capacity(len);
Expand Down Expand Up @@ -637,7 +637,7 @@ impl<'sig, 'read> Dec<'sig, 'read> {
let mut len_u = [0; _];
Self::align(self.read, &mut idx, dbus::Element::U32.dvar_alignment_exp())?;
Self::read(self.read, &mut idx, &mut len_u)?;
let len = self.format.to_u32(len_u) as usize;
let len = self.format.de_u32(len_u) as usize;

// Read the string.
let mut buffer = alloc::vec::Vec::with_capacity(len);
Expand All @@ -664,7 +664,7 @@ impl<'sig, 'read> Dec<'sig, 'read> {
pub fn u16(&mut self, data: &mut u16) -> Flow<Option<dbus::Error>, &mut Self> {
let mut v = [0; _];
self.fixed(dbus::Element::U16, &mut v)?;
*data = self.format.to_u16(v);
*data = self.format.de_u16(v);
Flow::Continue(self)
}

Expand All @@ -687,7 +687,7 @@ impl<'sig, 'read> Dec<'sig, 'read> {
let mut len_u = [0; _];
Self::align(self.read, &mut level.idx, dbus::Element::U32.dvar_alignment_exp())?;
Self::read(self.read, &mut level.idx, &mut len_u)?;
level.meta = self.format.to_u32(len_u) as usize;
level.meta = self.format.de_u32(len_u) as usize;
Self::align(self.read, &mut level.idx, align)?;

core::mem::swap(&mut level, &mut self.level);
Expand Down Expand Up @@ -739,6 +739,7 @@ impl<'sig, 'read> Dec<'sig, 'read> {
}
}

#[allow(clippy::octal_escapes)]
#[cfg(test)]
mod test {
use super::*;
Expand Down
8 changes: 5 additions & 3 deletions lib/tmp/src/fmt/dbus/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ struct ElementMeta {
gvar_size: u8,
}

#[allow(clippy::too_many_arguments)]
const fn element(
code: u8,
element: Element,
Expand Down Expand Up @@ -391,8 +392,9 @@ impl Element {
/// Create a new element from its id. This is a simple transmute that does
/// not modify the value.
///
/// SAFETY: `id` must be a valid element id gained from a call to
/// `Self::id()`.
/// ## Safety
///
/// `id` must be a valid element id gained from a call to `Self::id()`.
pub const unsafe fn from_id(id: u8) -> Self {
// SAFETY: The caller guarantees `id` was acquired via `Self::id()`,
// hence we can rely on `repr(u8)` to guarantee that we can
Expand Down Expand Up @@ -792,7 +794,7 @@ mod test {
assert!(!v.all(FLAG_OPEN | FLAG_CLOSE));
// ...has FLAG_OPEN or FLAG_CLOSE if it has a pair.
assert!(
!v.pair().is_some()
v.pair().is_none()
|| v.any(FLAG_OPEN | FLAG_CLOSE)
);
// ...has FLAG_CLOSE only if it has a pair.
Expand Down
1 change: 1 addition & 0 deletions lib/tmp/src/fmt/dbus/ende.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl<'sig, 'write> Enc<'sig, 'write> {

loop {
if dec.more() {
#[allow(clippy::match_single_binding)]
match cur.element().unwrap() {
_ => core::unreachable!(),
}
Expand Down
60 changes: 31 additions & 29 deletions lib/tmp/src/fmt/dbus/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
//! D-Bus type-system. These might not be well defined outside of this module.
//!
//! - *element*: An element is a single ASCII character that can be used in
//! a type signature. This can either refer to a primitive type, or be
//! part of a compound type signature. Hence, not all elements are
//! valid types by themselves, but might only be valid in combination
//! with other elements as part of a signature.
//! a type signature. This can either refer to a primitive type, or be
//! part of a compound type signature. Hence, not all elements are
//! valid types by themselves, but might only be valid in combination
//! with other elements as part of a signature.
//! - *primitive type*: A type with signature length of 1 is called a
//! primitive type.
//! primitive type.
//! - *compound type*: A non-primitive type is called a compound type.
//! - *bound container*: A bound container has an opening element, but no
//! closing element and thus forms a compound type with its following
//! single complete type.
//! closing element and thus forms a compound type with its following
//! single complete type.
//! - *unbound container*: An unbound container has both an opening and
//! closing element and thus forms a compound type with all its
//! enclosed types.
//! closing element and thus forms a compound type with all its
//! enclosed types.
//! - *DVariant*: The D-Bus type encoding as introduced in the original D-Bus
//! specification v0.8.
//! specification v0.8.
//! - *GVariant*: The D-Bus type encoding as introduced by glib.

// NB: This implementation avoids several standard Rust interfaces, because
Expand Down Expand Up @@ -170,14 +170,14 @@ enum NodeRef<'node> {
/// representations. The following representations are used by this module:
///
/// - `Sig<[u64]>`: This is the default representation, which can also be
/// referenced as `Sig`. It is a dynamically sized type (DST) and is
/// thus usually used behind a reference as `&Sig`.
/// This representation is used by default for any function that
/// required access to a D-Bus Signature.
/// referenced as `Sig`. It is a dynamically sized type (DST) and is
/// thus usually used behind a reference as `&Sig`.
/// This representation is used by default for any function that
/// required access to a D-Bus Signature.
/// - `Sig<[u64; N]>: This is a statically sized representation used to
/// create signatures on the stack. It uses const-generics to encode
/// the size of the data buffer.
/// This representation is used by default to create literals.
/// create signatures on the stack. It uses const-generics to encode
/// the size of the data buffer.
/// This representation is used by default to create literals.
#[repr(transparent)]
pub struct Sig<Nodes: ?Sized = [u64]> {
nodes: Nodes,
Expand Down Expand Up @@ -334,6 +334,7 @@ impl<'node> NodeRef<'node> {
macro_rules!
impl_node
{ ($node:ty, $int:ty, $id:expr) => {
#[allow(clippy::modulo_one)]
impl $node {
const ID: u8 = $id;
const SIZE_COEFF: usize = mem::size_of::<Self>() / mem::size_of::<u64>();
Expand Down Expand Up @@ -692,6 +693,7 @@ macro_rules!
impl_node!(Node8, u8, 0);
impl_node!(Node64, u64, 3);

#[allow(clippy::len_without_is_empty)]
impl Sig {
/// Calculate the required signature data buffer size given a signature
/// length.
Expand All @@ -705,9 +707,9 @@ impl Sig {
/// otherwise.
#[doc(hidden)]
pub const fn size_for_length(length: usize) -> usize {
assert!(mem::size_of::<Node8>() % mem::size_of::<u64>() == 0);
assert!(mem::size_of::<Node8>().is_multiple_of(mem::size_of::<u64>()));
assert!(mem::align_of::<Node8>() <= mem::align_of::<u64>());
assert!(mem::size_of::<Node64>() % mem::size_of::<u64>() == 0);
assert!(mem::size_of::<Node64>().is_multiple_of(mem::size_of::<u64>()));
assert!(mem::align_of::<Node64>() <= mem::align_of::<u64>());

if length <= Node8::LENGTH_MAX {
Expand Down Expand Up @@ -858,7 +860,7 @@ impl Sig {
Some(mem::transmute::<&[u64], &Self>(
// Use `split_at()` rather than range-indexing, since the
// latter is not available in const-fn.
&self.nodes
self.nodes
.split_at(idx.strict_mul(coeff)).1
.split_at(len.strict_mul(coeff)).0
))
Expand Down Expand Up @@ -910,7 +912,12 @@ impl<const SIZE: usize> Sig<[u64; SIZE]> {
// MSRV(unknown): This is available upstream as `transpose()` in
// `feature(maybe_uninit_uninit_array_transpose)` (#96097).
buf = mem::MaybeUninit::uninit();
buf_v = unsafe { mem::transmute(&mut buf) };
buf_v = unsafe {
mem::transmute::<
&mut mem::MaybeUninit<[u64; SIZE]>,
&mut [mem::MaybeUninit<u64>; SIZE],
>(&mut buf)
};

match Sig::<[u64]>::parse(buf_v, signature) {
// SAFETY: `Sig::parse()` always initializes the entire data
Expand Down Expand Up @@ -1193,7 +1200,7 @@ where
/// `None`.
pub fn dvar_alignment_exp(&self) -> Option<u8> {
self.flags_at(self.idx)
.and_then(|v| Some(v.dvar_alignment_exp()))
.map(|v| v.dvar_alignment_exp())
}

/// Return the GVar alignment exponent at the current index.
Expand All @@ -1205,7 +1212,7 @@ where
/// `None`.
pub fn gvar_alignment_exp(&self) -> Option<u8> {
self.flags_at(self.idx)
.and_then(|v| Some(v.gvar_alignment_exp()))
.map(|v| v.gvar_alignment_exp())
}
}

Expand Down Expand Up @@ -1305,12 +1312,7 @@ impl core::cmp::PartialEq for Sig {

impl core::cmp::PartialOrd for Sig {
fn partial_cmp(&self, v: &Self) -> Option<core::cmp::Ordering> {
// Sort by code to ensure stable order.
self.into_iter()
.map(|v| v.code())
.partial_cmp(
v.into_iter().map(|v| v.code()),
)
Some(self.cmp(v))
}
}

Expand Down
Loading