use core::{cmp, convert, fmt, mem};
use super::super::limb::{LIMB_BYTES, LimbType, ct_lsb_mask_l};
use super::{
MpMutUInt, MpMutUIntSlice, MpUInt, MpUIntSlice, MpUIntSliceCommon, ct_mp_limbs_align_len,
};
pub trait MpUIntCommonPriv: Sized {
const SUPPORTS_UNALIGNED_BUFFER_LENGTHS: bool;
fn _len(&self) -> usize;
fn partial_high_mask(&self) -> LimbType {
if Self::SUPPORTS_UNALIGNED_BUFFER_LENGTHS {
let high_npartial = if self._len() != 0 {
((self._len()) - 1) % LIMB_BYTES + 1
} else {
0
};
ct_lsb_mask_l(8 * high_npartial as u32)
} else {
!0
}
}
fn partial_high_shift(&self) -> u32 {
if Self::SUPPORTS_UNALIGNED_BUFFER_LENGTHS {
let high_npartial = self._len() % LIMB_BYTES;
if high_npartial == 0 {
0
} else {
8 * high_npartial as u32
}
} else {
0
}
}
}
pub trait MpUIntSliceCommonPriv: MpUIntCommonPriv {
type BackingSliceElementType: Sized + Copy + cmp::PartialEq + convert::From<u8> + fmt::Debug;
const BACKING_ELEMENT_SIZE: usize = mem::size_of::<Self::BackingSliceElementType>();
fn _limbs_align_len(nbytes: usize) -> usize {
if Self::SUPPORTS_UNALIGNED_BUFFER_LENGTHS {
nbytes
} else {
ct_mp_limbs_align_len(nbytes)
}
}
fn n_backing_elements_for_len(nbytes: usize) -> usize {
let nbytes = Self::_limbs_align_len(nbytes);
nbytes / Self::BACKING_ELEMENT_SIZE + (nbytes % Self::BACKING_ELEMENT_SIZE != 0) as usize
}
fn n_backing_elements(&self) -> usize;
fn take(self, nbytes: usize) -> (Self, Self);
}
pub trait MpUIntSlicePriv: MpUIntSliceCommon + MpUInt {
type SelfT<'a>: MpUIntSlice<BackingSliceElementType = Self::BackingSliceElementType>
where
Self: 'a;
type FromSliceError: fmt::Debug;
fn from_slice<'a: 'b, 'b>(
s: &'a [Self::BackingSliceElementType],
) -> Result<Self::SelfT<'b>, Self::FromSliceError>
where
Self: 'b;
fn _shrink_to(&self, nbytes: usize) -> Self::SelfT<'_>;
}
pub trait MpMutUIntSlicePriv: MpUIntSliceCommon + MpMutUInt {
type SelfT<'a>: MpMutUIntSlice<BackingSliceElementType = Self::BackingSliceElementType>
where
Self: 'a;
type FromSliceError: fmt::Debug;
fn from_slice<'a: 'b, 'b>(
s: &'a mut [Self::BackingSliceElementType],
) -> Result<Self::SelfT<'b>, Self::FromSliceError>
where
Self: 'b;
fn _shrink_to(&mut self, nbytes: usize) -> Self::SelfT<'_>;
}