Skip to content
3 changes: 3 additions & 0 deletions common/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl<'a, T: ?Sized> BorrowedValue<'a, T> {

impl<T: ?Sized> Deref for BorrowedValue<'_, T> {
type Target = T;

fn deref(&self) -> &T {
match self {
Self::Ref(r) => r,
Expand All @@ -81,6 +82,7 @@ pub enum BorrowedValueMut<'a, T: ?Sized> {
WriteLock(PyRwLockWriteGuard<'a, T>),
MappedWriteLock(PyMappedRwLockWriteGuard<'a, T>),
}

impl_from!('a, T, BorrowedValueMut<'a, T>,
RefMut(&'a mut T),
MuLock(PyMutexGuard<'a, T>),
Expand Down Expand Up @@ -108,6 +110,7 @@ impl<'a, T: ?Sized> BorrowedValueMut<'a, T> {

impl<T: ?Sized> Deref for BorrowedValueMut<'_, T> {
type Target = T;

fn deref(&self) -> &T {
match self {
Self::RefMut(r) => r,
Expand Down
16 changes: 10 additions & 6 deletions common/src/boxvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ impl<T> BoxVec<T> {
}

#[inline]
pub fn len(&self) -> usize {
pub const fn len(&self) -> usize {
self.len
}

#[inline]
pub fn is_empty(&self) -> bool {
pub const fn is_empty(&self) -> bool {
self.len() == 0
}

#[inline]
pub fn capacity(&self) -> usize {
pub const fn capacity(&self) -> usize {
self.xs.len()
}

pub fn is_full(&self) -> bool {
pub const fn is_full(&self) -> bool {
self.len() == self.capacity()
}

pub fn remaining_capacity(&self) -> usize {
pub const fn remaining_capacity(&self) -> usize {
self.capacity() - self.len()
}

Expand Down Expand Up @@ -336,6 +336,7 @@ impl<T> BoxVec<T> {

impl<T> Deref for BoxVec<T> {
type Target = [T];

#[inline]
fn deref(&self) -> &[T] {
unsafe { slice::from_raw_parts(self.as_ptr(), self.len()) }
Expand All @@ -354,6 +355,7 @@ impl<T> DerefMut for BoxVec<T> {
impl<'a, T> IntoIterator for &'a BoxVec<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self.iter()
}
Expand All @@ -363,6 +365,7 @@ impl<'a, T> IntoIterator for &'a BoxVec<T> {
impl<'a, T> IntoIterator for &'a mut BoxVec<T> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
Expand All @@ -374,6 +377,7 @@ impl<'a, T> IntoIterator for &'a mut BoxVec<T> {
impl<T> IntoIterator for BoxVec<T> {
type Item = T;
type IntoIter = IntoIter<T>;

fn into_iter(self) -> IntoIter<T> {
IntoIter { index: 0, v: self }
}
Expand Down Expand Up @@ -672,7 +676,7 @@ pub struct CapacityError<T = ()> {

impl<T> CapacityError<T> {
/// Create a new `CapacityError` from `element`.
pub fn new(element: T) -> CapacityError<T> {
pub const fn new(element: T) -> CapacityError<T> {
CapacityError { element }
}

Expand Down
27 changes: 21 additions & 6 deletions common/src/cformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ pub enum CFloatType {
}

impl CFloatType {
fn case(self) -> Case {
const fn case(self) -> Case {
use CFloatType::*;

match self {
ExponentLower | PointDecimalLower | GeneralLower => Case::Lower,
ExponentUpper | PointDecimalUpper | GeneralUpper => Case::Upper,
Expand All @@ -100,7 +101,7 @@ pub enum CFormatType {
}

impl CFormatType {
pub fn to_char(self) -> char {
pub const fn to_char(self) -> char {
match self {
CFormatType::Number(x) => x as u8 as char,
CFormatType::Float(x) => x as u8 as char,
Expand Down Expand Up @@ -136,9 +137,9 @@ bitflags! {
impl CConversionFlags {
#[inline]
pub fn sign_string(&self) -> &'static str {
if self.contains(CConversionFlags::SIGN_CHAR) {
if self.contains(Self::SIGN_CHAR) {
"+"
} else if self.contains(CConversionFlags::BLANK_SIGN) {
} else if self.contains(Self::BLANK_SIGN) {
" "
} else {
""
Expand Down Expand Up @@ -171,12 +172,15 @@ pub trait FormatChar: Copy + Into<CodePoint> + From<u8> {

impl FormatBuf for String {
type Char = char;

fn chars(&self) -> impl Iterator<Item = Self::Char> {
(**self).chars()
}

fn len(&self) -> usize {
self.len()
}

fn concat(mut self, other: Self) -> Self {
self.extend([other]);
self
Expand All @@ -187,19 +191,23 @@ impl FormatChar for char {
fn to_char_lossy(self) -> char {
self
}

fn eq_char(self, c: char) -> bool {
self == c
}
}

impl FormatBuf for Wtf8Buf {
type Char = CodePoint;

fn chars(&self) -> impl Iterator<Item = Self::Char> {
self.code_points()
}

fn len(&self) -> usize {
(**self).len()
}

fn concat(mut self, other: Self) -> Self {
self.extend([other]);
self
Expand All @@ -210,19 +218,23 @@ impl FormatChar for CodePoint {
fn to_char_lossy(self) -> char {
self.to_char_lossy()
}

fn eq_char(self, c: char) -> bool {
self == c
}
}

impl FormatBuf for Vec<u8> {
type Char = u8;

fn chars(&self) -> impl Iterator<Item = Self::Char> {
self.iter().copied()
}

fn len(&self) -> usize {
self.len()
}

fn concat(mut self, other: Self) -> Self {
self.extend(other);
self
Expand All @@ -233,6 +245,7 @@ impl FormatChar for u8 {
fn to_char_lossy(self) -> char {
self.into()
}

fn eq_char(self, c: char) -> bool {
char::from(self) == c
}
Expand Down Expand Up @@ -393,6 +406,7 @@ impl CFormatSpec {
Some(&(CFormatQuantity::Amount(1).into())),
)
}

pub fn format_bytes(&self, bytes: &[u8]) -> Vec<u8> {
let bytes = if let Some(CFormatPrecision::Quantity(CFormatQuantity::Amount(precision))) =
self.precision
Expand Down Expand Up @@ -706,12 +720,12 @@ pub enum CFormatPart<T> {

impl<T> CFormatPart<T> {
#[inline]
pub fn is_specifier(&self) -> bool {
pub const fn is_specifier(&self) -> bool {
matches!(self, CFormatPart::Spec { .. })
}

#[inline]
pub fn has_key(&self) -> bool {
pub const fn has_key(&self) -> bool {
match self {
CFormatPart::Spec(s) => s.mapping_key.is_some(),
_ => false,
Expand Down Expand Up @@ -803,6 +817,7 @@ impl<S> CFormatStrOrBytes<S> {
impl<S> IntoIterator for CFormatStrOrBytes<S> {
type Item = (usize, CFormatPart<S>);
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.parts.into_iter()
}
Expand Down
3 changes: 3 additions & 0 deletions common/src/encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl ops::Add for StrSize {
}
}
}

impl ops::AddAssign for StrSize {
fn add_assign(&mut self, rhs: Self) {
self.bytes += rhs.bytes;
Expand All @@ -133,6 +134,7 @@ struct DecodeError<'a> {
rest: &'a [u8],
err_len: Option<usize>,
}

/// # Safety
/// `v[..valid_up_to]` must be valid utf8
unsafe fn make_decode_err(v: &[u8], valid_up_to: usize, err_len: Option<usize>) -> DecodeError<'_> {
Expand All @@ -152,6 +154,7 @@ enum HandleResult<'a> {
reason: &'a str,
},
}

fn decode_utf8_compatible<Ctx, E, DecodeF, ErrF>(
mut ctx: Ctx,
errors: &E,
Expand Down
3 changes: 2 additions & 1 deletion common/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub mod windows {
}
}

fn attributes_to_mode(attr: u32) -> u16 {
const fn attributes_to_mode(attr: u32) -> u16 {
let mut m = 0;
if attr & FILE_ATTRIBUTE_DIRECTORY != 0 {
m |= libc::S_IFDIR | 0o111; // IFEXEC for user,group,other
Expand Down Expand Up @@ -362,6 +362,7 @@ pub mod windows {
}
}
}

pub fn stat_basic_info_to_stat(info: &FILE_STAT_BASIC_INFORMATION) -> StatStruct {
use windows_sys::Win32::Storage::FileSystem;
use windows_sys::Win32::System::Ioctl;
Expand Down
4 changes: 2 additions & 2 deletions common/src/float_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use malachite_bigint::{BigInt, ToBigInt};
use num_traits::{Float, Signed, ToPrimitive, Zero};
use std::f64;

pub fn decompose_float(value: f64) -> (f64, i32) {
pub const fn decompose_float(value: f64) -> (f64, i32) {
if 0.0 == value {
(0.0, 0i32)
} else {
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn gt_int(value: f64, other_int: &BigInt) -> bool {
}
}

pub fn div(v1: f64, v2: f64) -> Option<f64> {
pub const fn div(v1: f64, v2: f64) -> Option<f64> {
if v2 != 0.0 { Some(v1 / v2) } else { None }
}

Expand Down
5 changes: 3 additions & 2 deletions common/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl FormatSpec {
}
}

fn get_separator_interval(&self) -> usize {
const fn get_separator_interval(&self) -> usize {
match self.format_type {
Some(FormatType::Binary | FormatType::Octal | FormatType::Hex(_)) => 4,
Some(FormatType::Decimal | FormatType::Number(_) | FormatType::FixedPoint(_)) => 3,
Expand Down Expand Up @@ -677,7 +677,7 @@ struct AsciiStr<'a> {
}

impl<'a> AsciiStr<'a> {
fn new(inner: &'a str) -> Self {
const fn new(inner: &'a str) -> Self {
Self { inner }
}
}
Expand All @@ -690,6 +690,7 @@ impl CharLen for AsciiStr<'_> {

impl Deref for AsciiStr<'_> {
type Target = str;

fn deref(&self) -> &Self::Target {
self.inner
}
Expand Down
9 changes: 5 additions & 4 deletions common/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct HashSecret {

impl BuildHasher for HashSecret {
type Hasher = SipHasher24;

fn build_hasher(&self) -> Self::Hasher {
SipHasher24::new_with_keys(self.k0, self.k1)
}
Expand Down Expand Up @@ -80,7 +81,7 @@ impl HashSecret {
}

#[inline]
pub fn hash_pointer(value: usize) -> PyHash {
pub const fn hash_pointer(value: usize) -> PyHash {
// TODO: 32bit?
let hash = (value >> 4) | value;
hash as _
Expand Down Expand Up @@ -140,17 +141,17 @@ pub fn hash_bigint(value: &BigInt) -> PyHash {
}

#[inline]
pub fn hash_usize(data: usize) -> PyHash {
pub const fn hash_usize(data: usize) -> PyHash {
fix_sentinel(mod_int(data as i64))
}

#[inline(always)]
pub fn fix_sentinel(x: PyHash) -> PyHash {
pub const fn fix_sentinel(x: PyHash) -> PyHash {
if x == SENTINEL { -2 } else { x }
}

#[inline]
pub fn mod_int(value: i64) -> PyHash {
pub const fn mod_int(value: i64) -> PyHash {
value % MODULUS as i64
}

Expand Down
2 changes: 1 addition & 1 deletion common/src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn bytes_to_int(lit: &[u8], mut base: u32) -> Option<BigInt> {
}

#[inline]
pub fn detect_base(c: &u8) -> Option<u32> {
pub const fn detect_base(c: &u8) -> Option<u32> {
let base = match c {
b'x' | b'X' => 16,
b'b' | b'B' => 2,
Expand Down
Loading
Loading