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
PowerOfTwoLengthRequiredif 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
PowerOfTwoLengthRequiredif the number of values is not a power of two.IncorrectArgumentLengthif 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
IncorrectArgumentLengthif 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
IncorrectArgumentLengthif the number of field elements does not fit into thevalues.PowerOfTwoLengthRequiredif 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_checked(&self, index: usize) -> Result<P::Scalar, Error>
pub fn get_checked(&self, index: usize) -> Result<P::Scalar, Error>
Get a field element at the given index.
§Throws
Error::ArgumentRangeErrorif the index is out of bounds.
Sourcepub fn iter_scalars(
&self,
) -> impl Iterator<Item = P::Scalar> + Send + Clone + '_
pub fn iter_scalars( &self, ) -> impl Iterator<Item = P::Scalar> + Send + Clone + '_
Returns an iterator over the scalar elements in the buffer.
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>> + Clone, Error>
pub fn chunks( &self, log_chunk_size: usize, ) -> Result<impl Iterator<Item = FieldSlice<'_, P>> + Clone, Error>
Split the buffer into chunks of size 2^log_chunk_size.
§Errors
Error::ArgumentRangeErroriflog_chunk_size < P::LOG_WIDTHorlog_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::ArgumentRangeErroriflog_chunk_size > log_len.
Sourcepub fn split_half_ref(
&self,
) -> Result<(FieldSlice<'_, P>, FieldSlice<'_, P>), Error>
pub fn split_half_ref( &self, ) -> Result<(FieldSlice<'_, P>, FieldSlice<'_, P>), Error>
Splits the buffer in half and returns a pair of borrowed slices.
§Throws
Error::CannotSplitifself.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_checked(
&mut self,
index: usize,
value: P::Scalar,
) -> Result<(), Error>
pub fn set_checked( &mut self, index: usize, value: P::Scalar, ) -> Result<(), Error>
Set a field element at the given index.
§Throws
Error::ArgumentRangeErrorif 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::IncorrectArgumentLengthif 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::IncorrectArgumentLengthif 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::ArgumentRangeErroriflog_chunk_size < P::LOG_WIDTHorlog_chunk_size > log_len.
Sourcepub fn chunk_mut(
&mut self,
log_chunk_size: usize,
chunk_index: usize,
) -> Result<FieldBufferChunkMut<'_, P>, Error>
pub fn chunk_mut( &mut self, log_chunk_size: usize, chunk_index: usize, ) -> Result<FieldBufferChunkMut<'_, P>, Error>
Get a mutable aligned chunk of size 2^log_chunk_size.
This method behaves like FieldBuffer::chunk but returns a mutable reference.
For small chunks (log_chunk_size < P::LOG_WIDTH), this returns a wrapper that
implements deferred writes - modifications are written back when the wrapper is dropped.
§Throws
Error::ArgumentRangeErroriflog_chunk_size > log_lenorchunk_indexis out of range.
Sourcepub fn split_half(self) -> Result<FieldBufferSplitMut<P, Data>, Error>
pub fn split_half(self) -> Result<FieldBufferSplitMut<P, Data>, Error>
Consumes the buffer and splits it in half, returning a FieldBufferSplitMut.
This returns an object that owns the buffer data and can be used to access mutable references to the two halves. If the buffer contains a single packed element that needs to be split, the returned struct will create temporary copies and write the results back to the original buffer when dropped.
§Throws
Error::CannotSplitifself.log_len() == 0
Sourcepub fn split_half_mut(
&mut self,
) -> Result<FieldBufferSplitMut<P, &mut [P]>, Error>
pub fn split_half_mut( &mut self, ) -> Result<FieldBufferSplitMut<P, &mut [P]>, Error>
Splits the buffer in half and returns a FieldBufferSplitMut for accessing the halves.
This is a convenience method equivalent to self.to_mut().split_half().
§Throws
Error::CannotSplitifself.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, &'a mut [P]>
impl<'a, P: PackedField> FieldBuffer<P, &'a mut [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§
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