Skip to content

Commit 51f749d

Browse files
committed
fewer clippy warnings
1 parent f89514c commit 51f749d

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bit-struct"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
edition = "2021"
55
description = "Define structs which have fields which are assigned to individual bits, not bytes"
66
repository = "https://github.com/parallel-systems/bit-struct"

src/lib.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ pub struct GetSet<'a, P, T, const START: usize, const STOP: usize> {
146146

147147
impl<'a, P, T, const START: usize, const STOP: usize> GetSet<'a, P, T, START, STOP> {
148148
/// The bit offset at which this `GetSet` instance starts
149-
pub const fn start(&self) -> usize {
149+
#[must_use] pub const fn start(&self) -> usize {
150150
START
151151
}
152152

153153
/// The bit offset at which this `GetSet` instance ends
154-
pub const fn stop(&self) -> usize {
154+
#[must_use] pub const fn stop(&self) -> usize {
155155
STOP
156156
}
157157
}
@@ -206,7 +206,7 @@ impl<
206206
> GetSet<'a, P, T, START, STOP>
207207
{
208208
/// Get the property this `GetSet` points at
209-
pub fn get(&self) -> T {
209+
#[must_use] pub fn get(&self) -> T {
210210
let section = self.get_raw();
211211
// Safety:
212212
// This is guaranteed to be safe because the underlying storage must be bigger
@@ -216,14 +216,14 @@ impl<
216216

217217
/// Returns true if the memory this `GetSet` points at is a valid
218218
/// representation of `T`
219-
pub fn is_valid(&self) -> bool {
219+
#[must_use] pub fn is_valid(&self) -> bool {
220220
let section = self.get_raw();
221221
T::is_valid(section)
222222
}
223223

224224
/// Get the raw bits being pointed at, without type conversion nor any form
225225
/// of validation
226-
pub fn get_raw(&self) -> P {
226+
#[must_use] pub fn get_raw(&self) -> P {
227227
let parent = *self.parent;
228228
let mask = self.mask();
229229
(parent >> START) & mask
@@ -373,7 +373,7 @@ macro_rules! bit_struct_impl {
373373
/// A bit struct which has a zero value we can get
374374
pub trait BitStructZero: Zero {
375375
/// Get a zero value for this bit struct
376-
fn bs_zero() -> Self {
376+
#[must_use] fn bs_zero() -> Self {
377377
Self::zero()
378378
}
379379
}
@@ -540,6 +540,8 @@ macro_rules! bit_struct {
540540
impl $crate::BitStruct<{$(<$actual as $crate::ValidCheck<$kind>>::ALWAYS_VALID &&)* true}> for $name {
541541
type Kind = $kind;
542542

543+
/// # Safety
544+
/// - This is implemented automatically by the bit-struct crate.
543545
unsafe fn from_unchecked(inner: $kind) -> Self {
544546
Self(unsafe {$crate::UnsafeStorage::new_unsafe(inner)})
545547
}
@@ -548,12 +550,16 @@ macro_rules! bit_struct {
548550
#[allow(clippy::used_underscore_binding)]
549551
impl $name {
550552

553+
/// # Safety
554+
/// - This is implemented automatically by the bit-struct crate.
551555
unsafe fn from_unchecked(inner: $kind) -> Self {
552556
Self(unsafe {$crate::UnsafeStorage::new_unsafe(inner)})
553557
}
554558

555559
#[allow(clippy::too_many_arguments)]
556560
pub fn new($($field: $actual),*) -> Self {
561+
// SAFETY:
562+
// - This is implemented automatically by the bit-struct crate.
557563
let mut res = unsafe { Self::from_unchecked(<$kind as $crate::BitStructZero>::bs_zero()) };
558564
$(
559565
res.$field().set($field);
@@ -607,7 +613,7 @@ macro_rules! count_idents {
607613
/// assert_eq!(bits(5), 3);
608614
/// assert_eq!(bits(32), 6);
609615
/// ```
610-
pub const fn bits(num: usize) -> usize {
616+
#[must_use] pub const fn bits(num: usize) -> usize {
611617
/// Helper function for [`bits`]
612618
const fn helper(count: usize, on: usize) -> usize {
613619
// 0b11 = 3 log2_ceil(0b11) = 2 .. 2^2
@@ -637,6 +643,7 @@ macro_rules! enum_impl {
637643
};
638644
(VALID_CORE $name: ident: [$($kind: ty),*]) => {
639645
$(
646+
#[allow(clippy::undocumented_unsafe_blocks, clippy::use_self)]
640647
unsafe impl $crate::ValidCheck<$kind> for $name {
641648
const ALWAYS_VALID: bool = <Self as $crate::ValidCheck<u8>>::ALWAYS_VALID;
642649
fn is_valid(value: $kind) -> bool {
@@ -653,6 +660,7 @@ macro_rules! enum_impl {
653660
};
654661
(VALID_BIT_STRUCT $name: ident: [$($kind: ty),*]) => {
655662
$(
663+
#[allow(clippy::undocumented_unsafe_blocks, clippy::use_self)]
656664
unsafe impl $crate::ValidCheck<$kind> for $name {
657665
const ALWAYS_VALID: bool = <Self as $crate::ValidCheck<u8>>::ALWAYS_VALID;
658666
fn is_valid(value: $kind) -> bool {
@@ -709,14 +717,19 @@ macro_rules! enum_impl {
709717
),*
710718
}
711719

720+
/// # Safety
721+
/// - This is implemented automatically by the bit-struct crate.
722+
#[allow(clippy::use_self)]
712723
unsafe impl $crate::BitCount for $name {
713724
const COUNT: usize = $crate::bits($crate::count_idents!(0, [$($field),*]));
714725
}
715726

727+
#[allow(clippy::use_self)]
716728
impl $name {
717729
const VARIANT_COUNT: usize = $crate::enum_impl!(COUNT $fst_field $(,$field)*);
718730
}
719731

732+
#[allow(clippy::undocumented_unsafe_blocks, clippy::use_self)]
720733
unsafe impl $crate::ValidCheck<u8> for $name {
721734
const ALWAYS_VALID: bool = Self::VARIANT_COUNT.count_ones() == 1;
722735
fn is_valid(value: u8) -> bool {
@@ -730,6 +743,7 @@ macro_rules! enum_impl {
730743

731744
$crate::enum_impl!(FROM_IMPLS $name);
732745

746+
#[allow(clippy::use_self)]
733747
impl Default for $name {
734748
fn default() -> Self {
735749
Self::$default
@@ -762,6 +776,7 @@ macro_rules! enum_impl {
762776
),*
763777
}
764778

779+
#[allow(clippy::use_self)]
765780
impl Default for $name {
766781
fn default() -> Self {
767782
Self::$fst_field
@@ -772,11 +787,12 @@ macro_rules! enum_impl {
772787
const VARIANT_COUNT: usize = $crate::enum_impl!(COUNT $fst_field $(,$field)*);
773788
}
774789

790+
#[allow(clippy::undocumented_unsafe_blocks)]
775791
unsafe impl $crate::BitCount for $name {
776792
const COUNT: usize = $crate::bits($crate::count_idents!(0, [$($field),*]));
777793
}
778794

779-
795+
#[allow(clippy::undocumented_unsafe_blocks)]
780796
unsafe impl $crate::ValidCheck<u8> for $name {
781797
const ALWAYS_VALID: bool = Self::VARIANT_COUNT.count_ones() == 1;
782798

src/types.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
//! New integer types used in this crate, and trait implementations for those
22
//! types
33
4-
use super::*;
4+
use super::{
5+
serde, Add, BitAnd, BitAndAssign, BitCount, BitOr, BitOrAssign, BitXor, BitXorAssign,
6+
BitsFitIn, Bounded, Debug, Display, Div, FieldStorage, Mul, Num, One, Rem, Shl, ShlAssign, Shr,
7+
ShrAssign, Sub, ValidCheck, Zero,
8+
};
9+
use crate::enums;
510
use serde::{Deserializer, Serializer};
611

712
/// Assert that the given type is valid for any representation thereof
@@ -54,6 +59,7 @@ impl_field_storage!(
5459
(u64, Self),
5560
(u128, Self),
5661
);
62+
/// helper macro to implement signed types
5763
macro_rules! impl_signed_field_storage {
5864
($(($type:ty, $base:ty)),+ $(,)?) => {
5965
$(
@@ -156,7 +162,7 @@ macro_rules! new_signed_types {
156162
/// Create a new $name from value
157163
/// # Safety
158164
/// - value must fit within the number of bits defined in the type
159-
pub const unsafe fn new_unchecked(value: $signed) -> Self {
165+
#[must_use] pub const unsafe fn new_unchecked(value: $signed) -> Self {
160166
let unsigned_value = value as $inner;
161167
if value >= 0 {
162168
Self(unsigned_value)
@@ -171,7 +177,7 @@ macro_rules! new_signed_types {
171177
/// Create a new $name from value
172178
/// # Safety
173179
/// - value must fit within the number of bits defined in the type
174-
pub fn new(value: $signed) -> Option<Self> {
180+
#[must_use] pub fn new(value: $signed) -> Option<Self> {
175181
if (Self::MIN..=Self::MAX).contains(&value) {
176182
// SAFETY:
177183
// We've just checked that this is safe to call
@@ -189,7 +195,7 @@ macro_rules! new_signed_types {
189195
pub const MIN: $signed = -(Self::MAX_UNSIGNED as $signed) - 1;
190196

191197
/// Get the value stored in here, as a signed integer
192-
pub const fn value(self) -> $signed {
198+
#[must_use] pub const fn value(self) -> $signed {
193199
match self.0 >> ($count - 1) {
194200
0 => self.0 as $signed,
195201
_ => {
@@ -467,14 +473,14 @@ macro_rules! new_unsigned_types {
467473
///
468474
/// # Safety
469475
/// The value must be valid value of the given type.
470-
pub const unsafe fn new_unchecked(value: $inner) -> Self {
476+
#[must_use] pub const unsafe fn new_unchecked(value: $inner) -> Self {
471477
Self(value)
472478
}
473479

474480
#[doc = concat!("Create a new ", stringify!($name), " from an inner value")]
475481
///
476482
/// This method checks that the inner value is valid, and return `None` if it isn't.
477-
pub fn new(value: $inner) -> Option<Self> {
483+
#[must_use] pub fn new(value: $inner) -> Option<Self> {
478484
if (Self::MIN..=Self::MAX).contains(&value) {
479485
// SAFETY:
480486
// We've checked that this is safe to do in the above `if`
@@ -485,7 +491,7 @@ macro_rules! new_unsigned_types {
485491
}
486492

487493
/// Get the stored value
488-
pub const fn value(self) -> $inner {
494+
#[must_use] pub const fn value(self) -> $inner {
489495
self.0
490496
}
491497
}
@@ -646,7 +652,7 @@ macro_rules! byte_from_impls {
646652
/// The size of byte array equal to the underlying storage for this value
647653
const SUPER_BYTES: usize = ::core::mem::size_of::<$super_kind>();
648654
/// Convert from an array of bytes, in big-endian order
649-
pub fn from_be_bytes(bytes: [u8; Self::ARR_SIZE]) -> Self {
655+
#[must_use] pub fn from_be_bytes(bytes: [u8; Self::ARR_SIZE]) -> Self {
650656
let mut res_bytes = [0_u8; Self::SUPER_BYTES];
651657
for (set, &get) in res_bytes.iter_mut().rev().zip(bytes.iter().rev()) {
652658
*set = get;
@@ -655,7 +661,7 @@ macro_rules! byte_from_impls {
655661
}
656662

657663
/// Convert `self` into an array of bytes, in big-endian order
658-
pub fn to_be_bytes(self) -> [u8; Self::ARR_SIZE] {
664+
#[must_use] pub fn to_be_bytes(self) -> [u8; Self::ARR_SIZE] {
659665
let mut res = [0; Self::ARR_SIZE];
660666
let inner_bytes = self.0.to_be_bytes();
661667
for (&get, set) in inner_bytes.iter().rev().zip(res.iter_mut().rev()) {
@@ -665,7 +671,7 @@ macro_rules! byte_from_impls {
665671
}
666672

667673
/// Convert from an array of bytes, in little-endian order
668-
pub fn from_le_bytes(bytes: [u8; Self::ARR_SIZE]) -> Self {
674+
#[must_use] pub fn from_le_bytes(bytes: [u8; Self::ARR_SIZE]) -> Self {
669675
let mut res_bytes = [0_u8; Self::SUPER_BYTES];
670676
for (set, &get) in res_bytes.iter_mut().zip(bytes.iter()) {
671677
*set = get;
@@ -674,7 +680,7 @@ macro_rules! byte_from_impls {
674680
}
675681

676682
/// Convert `self` into an array of bytes, in little-endian order
677-
pub fn to_le_bytes(self) -> [u8; Self::ARR_SIZE] {
683+
#[must_use] pub fn to_le_bytes(self) -> [u8; Self::ARR_SIZE] {
678684
let mut res = [0; Self::ARR_SIZE];
679685
let inner_bytes = self.0.to_le_bytes();
680686
for (&get, set) in inner_bytes.iter().zip(res.iter_mut()) {
@@ -724,13 +730,15 @@ impl u1 {
724730
/// Implement `BitsFitIn` for the given pair of types, using the given method
725731
macro_rules! bits_fit_in_impl {
726732
($basety:ty => $target:ty : from) => {
733+
#[allow(clippy::use_self)]
727734
impl BitsFitIn<$target> for $basety {
728735
fn fit(self) -> $target {
729736
self.inner_raw().into()
730737
}
731738
}
732739
};
733740
($basety:ty => $target:ty : new_unchecked) => {
741+
#[allow(clippy::use_self)]
734742
impl BitsFitIn<$target> for $basety {
735743
fn fit(self) -> $target {
736744
// Safety:

0 commit comments

Comments
 (0)