pub struct FieldBuffer<P: PackedField, Data: Deref<Target = [P]> = Box<[P]>> { /* private fields */ }
Expand description
A power-of-two-sized buffer containing field elements, stored in packed fields.
This struct maintains a set of invariants:
values.len()
is a power of twovalues.len() >= 1 << log_len.saturating_sub(P::LOG_WIDTH)
.
Implementations§
Source§impl<P: PackedField> FieldBuffer<P>
impl<P: PackedField> FieldBuffer<P>
Sourcepub fn from_values(values: &[P::Scalar]) -> Result<Self, Error>
pub fn from_values(values: &[P::Scalar]) -> Result<Self, Error>
Create a new FieldBuffer from a vector of values.
§Throws
PowerOfTwoLengthRequired
if the number of values is not a power of two.
Sourcepub fn from_values_truncated(
values: &[P::Scalar],
log_cap: usize,
) -> Result<Self, Error>
pub fn from_values_truncated( values: &[P::Scalar], log_cap: usize, ) -> Result<Self, Error>
Create a new FieldBuffer from a vector of values.
Capacity log_cap
is bumped to at least P::LOG_WIDTH
.
§Throws
PowerOfTwoLengthRequired
if the number of values is not a power of two.IncorrectArgumentLength
if the number of values exceeds1 << log_cap
.
Sourcepub fn zeros(log_len: usize) -> Self
pub fn zeros(log_len: usize) -> Self
Create a new FieldBuffer
of zeros with the given log_len.
Sourcepub fn zeros_truncated(log_len: usize, log_cap: usize) -> Result<Self, Error>
pub fn zeros_truncated(log_len: usize, log_cap: usize) -> Result<Self, Error>
Create a new FieldBuffer
of zeros with the given log_len and capacity log_cap.
Capacity log_cap
is bumped to at least P::LOG_WIDTH
.
Source§impl<P: PackedField, Data: Deref<Target = [P]>> FieldBuffer<P, Data>
impl<P: PackedField, Data: Deref<Target = [P]>> FieldBuffer<P, Data>
Sourcepub fn new(log_len: usize, values: Data) -> Result<Self, Error>
pub fn new(log_len: usize, values: Data) -> Result<Self, Error>
Create a new FieldBuffer from a slice of packed values.
§Throws
IncorrectArgumentLength
if the number of field elements does not fit thevalues.len()
exactly.
Sourcepub fn new_truncated(log_len: usize, values: Data) -> Result<Self, Error>
pub fn new_truncated(log_len: usize, values: Data) -> Result<Self, Error>
Create a new FieldBuffer from a slice of packed values.
§Throws
IncorrectArgumentLength
if the number of field elements does not fit into thevalues
.PowerOfTwoLengthRequired
if thevalues.len()
is not a power of two.
Sourcepub fn log_cap(&self) -> usize
pub fn log_cap(&self) -> usize
Returns log2 the number of field elements that the underlying collection may take.
Sourcepub fn cap(&self) -> usize
pub fn cap(&self) -> usize
Returns the number of field elements that the underlying collection may take.
Sourcepub fn to_ref(&self) -> FieldSlice<'_, P>
pub fn to_ref(&self) -> FieldSlice<'_, P>
Borrows the buffer as a FieldSlice
.
Sourcepub fn get(&self, index: usize) -> Result<P::Scalar, Error>
pub fn get(&self, index: usize) -> Result<P::Scalar, Error>
Get a field element at the given index.
§Throws
Error::ArgumentRangeError
if the index is out of bounds.
Sourcepub fn chunk(
&self,
log_chunk_size: usize,
chunk_index: usize,
) -> Result<FieldSlice<'_, P>, Error>
pub fn chunk( &self, log_chunk_size: usize, chunk_index: usize, ) -> Result<FieldSlice<'_, P>, Error>
Get an aligned chunk of size 2^log_chunk_size
.
Chunk start offset divides chunk size; the result is essentially
chunks(log_chunk_size).nth(chunk_index)
but unlike chunks
it does
support sizes smaller than packing width.
Sourcepub fn chunks(
&self,
log_chunk_size: usize,
) -> Result<impl Iterator<Item = FieldSlice<'_, P>>, Error>
pub fn chunks( &self, log_chunk_size: usize, ) -> Result<impl Iterator<Item = FieldSlice<'_, P>>, Error>
Split the buffer into chunks of size 2^log_chunk_size
.
§Errors
Error::ArgumentRangeError
iflog_chunk_size < P::LOG_WIDTH
orlog_chunk_size > log_len
.
Sourcepub fn chunks_par(
&self,
log_chunk_size: usize,
) -> Result<impl IndexedParallelIterator<Item = FieldSlice<'_, P>>, Error>
pub fn chunks_par( &self, log_chunk_size: usize, ) -> Result<impl IndexedParallelIterator<Item = FieldSlice<'_, P>>, Error>
Creates an iterator over chunks of size 2^log_chunk_size
in parallel.
§Throws
Error::ArgumentRangeError
iflog_chunk_size < P::LOG_WIDTH
orlog_chunk_size > log_len
.
Sourcepub fn split_half(
&self,
) -> Result<(FieldSlice<'_, P>, FieldSlice<'_, P>), Error>
pub fn split_half( &self, ) -> Result<(FieldSlice<'_, P>, FieldSlice<'_, P>), Error>
Splits the buffer in half and returns a pair of borrowed slices.
§Throws
Error::CannotSplit
ifself.log_len() == 0
Source§impl<P: PackedField, Data: DerefMut<Target = [P]>> FieldBuffer<P, Data>
impl<P: PackedField, Data: DerefMut<Target = [P]>> FieldBuffer<P, Data>
Sourcepub fn to_mut(&mut self) -> FieldSliceMut<'_, P>
pub fn to_mut(&mut self) -> FieldSliceMut<'_, P>
Borrows the buffer mutably as a FieldSliceMut
.
Sourcepub fn set(&mut self, index: usize, value: P::Scalar) -> Result<(), Error>
pub fn set(&mut self, index: usize, value: P::Scalar) -> Result<(), Error>
Set a field element at the given index.
§Throws
Error::ArgumentRangeError
if the index is out of bounds.
Sourcepub fn truncate(&mut self, new_log_len: usize)
pub fn truncate(&mut self, new_log_len: usize)
Truncates a field buffer to a shorter length.
If new_log_len
is not less than current log_len()
, this has no effect.
Sourcepub fn zero_extend(&mut self, new_log_len: usize) -> Result<(), Error>
pub fn zero_extend(&mut self, new_log_len: usize) -> Result<(), Error>
Zero extends a field buffer to a longer length.
If new_log_len
is not greater than current log_len()
, this has no effect.
§Throws
Error::IncorrectArgumentLength
if the zero extended size exceeds underlying capacity.
Sourcepub fn resize(&mut self, new_log_len: usize) -> Result<(), Error>
pub fn resize(&mut self, new_log_len: usize) -> Result<(), Error>
Sets the new log length. If the new log length is bigger than the current log length,
the new values (in case when self.log_len < new_log_len
) will be filled with
the values from the existing buffer.
§Throws
Error::IncorrectArgumentLength
if the new log length exceeds the buffer’s capacity.
Sourcepub fn chunks_mut(
&mut self,
log_chunk_size: usize,
) -> Result<impl Iterator<Item = FieldSliceMut<'_, P>>, Error>
pub fn chunks_mut( &mut self, log_chunk_size: usize, ) -> Result<impl Iterator<Item = FieldSliceMut<'_, P>>, Error>
Split the buffer into mutable chunks of size 2^log_chunk_size
.
§Throws
Error::ArgumentRangeError
iflog_chunk_size < P::LOG_WIDTH
orlog_chunk_size > log_len
.
Sourcepub fn split_half_mut<F, R>(&mut self, f: F) -> Result<R, Error>
pub fn split_half_mut<F, R>(&mut self, f: F) -> Result<R, Error>
Splits the buffer in half and calls a closure with the two halves.
If the buffer contains a single packed element that needs to be split, this method will create temporary copies, call the closure, and then write the results back to the original buffer.
§Throws
Error::CannotSplit
ifself.log_len() == 0
Sourcepub fn split_half_mut_no_closure(
&mut self,
) -> Result<FieldBufferSplitMut<'_, P>, Error>
pub fn split_half_mut_no_closure( &mut self, ) -> Result<FieldBufferSplitMut<'_, P>, Error>
Splits the buffer in half and returns a FieldBufferSplitMut
for accessing the halves.
This returns an object that can be used to access mutable references to the two halves.
This method unfortunately can’t simply return a tuple of slices because the buffer may have
only one packed element. If the buffer contains a single packed element that needs to be
split, this method will create temporary copies, call the closure, and then write the
results back to the original buffer when the returned FieldBufferSplitMut
is dropped.
§Throws
Error::CannotSplit
ifself.log_len() == 0
Source§impl<'a, P: PackedField> FieldBuffer<P, FieldSliceData<'a, P>>
impl<'a, P: PackedField> FieldBuffer<P, FieldSliceData<'a, P>>
Source§impl<'a, P: PackedField> FieldBuffer<P, FieldSliceDataMut<'a, P>>
impl<'a, P: PackedField> FieldBuffer<P, FieldSliceDataMut<'a, P>>
Trait Implementations§
Source§impl<P: Clone + PackedField, Data: Clone + Deref<Target = [P]>> Clone for FieldBuffer<P, Data>
impl<P: Clone + PackedField, Data: Clone + Deref<Target = [P]>> Clone for FieldBuffer<P, Data>
Source§fn clone(&self) -> FieldBuffer<P, Data>
fn clone(&self) -> FieldBuffer<P, Data>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<P: Debug + PackedField, Data: Debug + Deref<Target = [P]>> Debug for FieldBuffer<P, Data>
impl<P: Debug + PackedField, Data: Debug + Deref<Target = [P]>> Debug for FieldBuffer<P, Data>
Source§impl<'a, P: PackedField, Data: Deref<Target = [P]>> From<&'a FieldBuffer<P, Data>> for FieldSlice<'a, P>
impl<'a, P: PackedField, Data: Deref<Target = [P]>> From<&'a FieldBuffer<P, Data>> for FieldSlice<'a, P>
Source§fn from(buffer: &'a FieldBuffer<P, Data>) -> Self
fn from(buffer: &'a FieldBuffer<P, Data>) -> Self
Source§impl<'a, P: PackedField, Data: DerefMut<Target = [P]>> From<&'a mut FieldBuffer<P, Data>> for FieldSliceMut<'a, P>
impl<'a, P: PackedField, Data: DerefMut<Target = [P]>> From<&'a mut FieldBuffer<P, Data>> for FieldSliceMut<'a, P>
Source§fn from(buffer: &'a mut FieldBuffer<P, Data>) -> Self
fn from(buffer: &'a mut FieldBuffer<P, Data>) -> Self
impl<P: Eq + PackedField, Data: Eq + Deref<Target = [P]>> Eq for FieldBuffer<P, Data>
Auto Trait Implementations§
impl<P, Data> Freeze for FieldBuffer<P, Data>where
Data: Freeze,
impl<P, Data> RefUnwindSafe for FieldBuffer<P, Data>where
Data: RefUnwindSafe,
impl<P, Data> Send for FieldBuffer<P, Data>where
Data: Send,
impl<P, Data> Sync for FieldBuffer<P, Data>where
Data: Sync,
impl<P, Data> Unpin for FieldBuffer<P, Data>where
Data: Unpin,
impl<P, Data> UnwindSafe for FieldBuffer<P, Data>where
Data: UnwindSafe,
Blanket Implementations§
§impl<T> AsMaybeUninit for T
impl<T> AsMaybeUninit for T
§type Uninit = MaybeUninit<T>
type Uninit = MaybeUninit<T>
§fn as_ref_uninit(&self) -> &<T as AsMaybeUninit>::Uninit
fn as_ref_uninit(&self) -> &<T as AsMaybeUninit>::Uninit
&self
to its maybe-initialized equivalent.§unsafe fn as_mut_uninit(&mut self) -> &mut <T as AsMaybeUninit>::Uninit
unsafe fn as_mut_uninit(&mut self) -> &mut <T as AsMaybeUninit>::Uninit
&mut T
to its maybe-initialized equivalent. Read more§unsafe fn raw_as_uninit<'a>(raw: *const T) -> &'a <T as AsMaybeUninit>::Uninit
unsafe fn raw_as_uninit<'a>(raw: *const T) -> &'a <T as AsMaybeUninit>::Uninit
§unsafe fn raw_mut_as_uninit<'a>(
raw: *mut T,
) -> &'a mut <T as AsMaybeUninit>::Uninit
unsafe fn raw_mut_as_uninit<'a>( raw: *mut T, ) -> &'a mut <T as AsMaybeUninit>::Uninit
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more