Skip to content

Commit 42b4356

Browse files
committed
move fill value repeat into ArrayBytesFixedDisjointView::fill
1 parent 99ba588 commit 42b4356

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

zarrs/src/array/array_bytes_fixed_disjoint_view.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,33 +194,31 @@ impl<'a> ArrayBytesFixedDisjointView<'a> {
194194
self.contiguous_indices().contiguous_elements_usize() * self.data_type_size
195195
}
196196

197-
/// Fill the view with a constant per contiguous slice.
198-
///
199-
/// The constant value must be the same length as the byte length of contiguous elements in the view.
197+
/// Fill the view with the fill value.
200198
///
201199
/// # Errors
202-
/// Returns [`InvalidBytesLengthError`] if the length of `fill_value_contiguous` does not match [`Self::contiguous_bytes_len`].
200+
/// Returns [`InvalidBytesLengthError`] if the length of the `fill_value` does not match the data type size.
203201
///
204202
/// # Panics
205203
/// Panics if an offset into the internal bytes reference exceeds [`usize::MAX`].
206-
pub fn copy_from_slice_to_contiguous_elements(
207-
&mut self,
208-
fill_value_contiguous: &[u8],
209-
) -> Result<(), InvalidBytesLengthError> {
210-
let contiguous_indices = self.contiguous_linearised_indices();
211-
let length = self.contiguous_bytes_len();
212-
if fill_value_contiguous.len() != length {
204+
pub fn fill(&mut self, fill_value: &[u8]) -> Result<(), InvalidBytesLengthError> {
205+
if fill_value.len() != self.data_type_size {
213206
return Err(InvalidBytesLengthError::new(
214-
fill_value_contiguous.len(),
215-
length,
207+
fill_value.len(),
208+
self.data_type_size,
216209
));
217210
}
211+
212+
let fill_value_contiguous = fill_value.repeat(self.num_contiguous_elements());
213+
let length = self.contiguous_bytes_len();
214+
debug_assert_eq!(fill_value_contiguous.len(), length);
215+
let contiguous_indices = self.contiguous_linearised_indices();
218216
contiguous_indices.into_iter().for_each(|index| {
219217
let offset = usize::try_from(index * self.data_type_size as u64).unwrap();
220218
unsafe {
221219
self.bytes
222220
.index_mut(offset..offset + length)
223-
.copy_from_slice(fill_value_contiguous);
221+
.copy_from_slice(&fill_value_contiguous);
224222
}
225223
});
226224
Ok(())

zarrs/src/array/codec/array_to_bytes/sharding/sharding_codec.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,8 @@ impl ArrayToBytesCodecTraits for ShardingCodec {
273273
let offset = shard_index[chunk_index * 2];
274274
let size = shard_index[chunk_index * 2 + 1];
275275
if offset == u64::MAX && size == u64::MAX {
276-
let fill_value_contiguous = shard_representation
277-
.fill_value()
278-
.as_ne_bytes()
279-
.repeat(output_view_inner_chunk.num_contiguous_elements());
280276
output_view_inner_chunk
281-
.copy_from_slice_to_contiguous_elements(&fill_value_contiguous)?;
277+
.fill(shard_representation.fill_value().as_ne_bytes())?;
282278
} else if usize::try_from(offset + size).unwrap() > encoded_shard.len() {
283279
return Err(CodecError::Other(
284280
"The shard index references out-of-bounds bytes. The chunk may be corrupted."
@@ -371,12 +367,7 @@ impl ArrayToBytesCodecTraits for ShardingCodec {
371367
let offset = shard_index[chunk_index * 2];
372368
let size = shard_index[chunk_index * 2 + 1];
373369
if offset == u64::MAX && size == u64::MAX {
374-
let fill_value_contiguous = shard_representation
375-
.fill_value()
376-
.as_ne_bytes()
377-
.repeat(output_view_inner_chunk.num_contiguous_elements());
378-
output_view_inner_chunk
379-
.copy_from_slice_to_contiguous_elements(&fill_value_contiguous)?;
370+
output_view_inner_chunk.fill(shard_representation.fill_value().as_ne_bytes())?;
380371
} else if usize::try_from(offset + size).unwrap() > encoded_shard.len() {
381372
return Err(CodecError::Other(
382373
"The shard index references out-of-bounds bytes. The chunk may be corrupted."

zarrs/src/array/codec/array_to_bytes/sharding/sharding_partial_decoder.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,8 @@ impl AsyncArrayPartialDecoderTraits for AsyncShardingPartialDecoder {
651651
.unwrap(),
652652
)
653653
};
654-
let fill_value_contiguous = self
655-
.decoded_representation
656-
.fill_value()
657-
.as_ne_bytes()
658-
.repeat(output_view.num_contiguous_elements());
659654
output_view
660-
.copy_from_slice_to_contiguous_elements(&fill_value_contiguous)
655+
.fill(self.decoded_representation.fill_value().as_ne_bytes())
661656
.map_err(CodecError::from)
662657
}
663658
)?;

0 commit comments

Comments
 (0)