Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/std/bound_coord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ macro_rules! wrap {

#[inline]
#[must_use]
pub const fn value(&self) -> $ty {
pub fn value(&self) -> $ty {
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
unsafe {
core::hint::assert_unchecked(self.value < self.domain);
}
self.value
}

Expand Down
14 changes: 11 additions & 3 deletions src/std/bound_numerics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ where
#[inline]
#[must_use]
pub fn new(domain: T) -> Option<Self> {
if domain == ConstZero::ZERO {
if domain <= ConstZero::ZERO {
return None;
}
Some(Self { domain })
}

#[inline]
#[must_use]
pub const fn domain(&self) -> &T {
pub fn domain(&self) -> &T {
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
unsafe {
core::hint::assert_unchecked(self.domain > ConstZero::ZERO);
}
&self.domain
}
}
Expand Down Expand Up @@ -64,7 +68,11 @@ where

#[inline]
#[must_use]
pub const fn value(&self) -> &T {
pub fn value(&self) -> &T {
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
unsafe {
core::hint::assert_unchecked(self.value < *self.domain);
}
&self.value
}

Expand Down
1 change: 0 additions & 1 deletion src/std/ndslice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ workspace = true
[dependencies]
rawspeed-common-exact_ops = { workspace = true }
rawspeed-std = { workspace = true }
rawspeed-std-range_rotation = { workspace = true }

[lib]
path = "mod.rs"
Expand Down
39 changes: 15 additions & 24 deletions src/std/ndslice/offsetarray2dref/iteration.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use crate::offsetarray2dref::OffsetArray2DRef;
use rawspeed_std::coord_common::{ColIndex, Coord2D, RowIndex};
use rawspeed_std_range_rotation::range_rotation::rotated_range::{
RotatableRange as _, RotatedRange,
};

use crate::{array2dref::Array2DRef, offsetarray2dref::OffsetArray2DRef};

#[non_exhaustive]
#[must_use]
Expand All @@ -17,7 +13,7 @@ pub struct Rows<'a, 'b, T> {
#[derive(Debug, Clone)]
pub struct RowIterator<'a, 'b, T> {
underlying: &'b OffsetArray2DRef<'a, T>,
rotated_rows: <RotatedRange<usize> as IntoIterator>::IntoIter,
rows: <core::ops::Range<usize> as IntoIterator>::IntoIter,
}

#[non_exhaustive]
Expand All @@ -40,14 +36,14 @@ pub struct Columns<'a, 'b, 'c, T> {
#[derive(Debug, Clone)]
pub struct ColumnIterator<'a, 'b, 'c, T> {
underlying: &'c Row<'a, 'b, T>,
rotated_cols: <RotatedRange<usize> as IntoIterator>::IntoIter,
cols: <core::ops::Range<usize> as IntoIterator>::IntoIter,
}

#[non_exhaustive]
#[must_use]
#[derive(Debug, Clone, Copy)]
pub struct Element<'a, 'b, T> {
underlying: &'b Array2DRef<'a, T>,
underlying: &'b OffsetArray2DRef<'a, T>,
coord: Coord2D,
}

Expand Down Expand Up @@ -75,12 +71,8 @@ impl<'a, 'b, T> RowIterator<'a, 'b, T> {
#[inline]
fn new(underlying: &'b OffsetArray2DRef<'a, T>) -> Self {
let rows = 0..underlying.num_rows().get();
let rotated_rows = rows.rotate(*underlying.origin.row());
let rotated_rows = rotated_rows.into_iter();
Self {
underlying,
rotated_rows,
}
let rows = rows.into_iter();
Self { underlying, rows }
}
}

Expand All @@ -90,7 +82,7 @@ impl<'a, 'b, T> Iterator for RowIterator<'a, 'b, T> {

#[inline]
fn next(&mut self) -> Option<Self::Item> {
let row = self.rotated_rows.next()?;
let row = self.rows.next()?;
Some(Row::new(self.underlying, RowIndex::new(row)))
}
}
Expand Down Expand Up @@ -132,12 +124,8 @@ impl<'a, 'b, 'c, T> ColumnIterator<'a, 'b, 'c, T> {
#[inline]
fn new(underlying: &'c Row<'a, 'b, T>) -> Self {
let cols = 0..underlying.underlying.row_length().get();
let rotated_cols = cols.rotate(*underlying.underlying.origin.col());
let rotated_cols = rotated_cols.into_iter();
Self {
underlying,
rotated_cols,
}
let cols = cols.into_iter();
Self { underlying, cols }
}
}

Expand All @@ -147,15 +135,18 @@ impl<'a, 'b, T> Iterator for ColumnIterator<'a, 'b, '_, T> {

#[inline]
fn next(&mut self) -> Option<Self::Item> {
let col = self.rotated_cols.next()?;
let col = self.cols.next()?;
let coord = Coord2D::new(self.underlying.row, ColIndex::new(col));
Some(Element::new(&self.underlying.underlying.data, coord))
Some(Element::new(self.underlying.underlying, coord))
}
}

impl<'a, 'b, T> Element<'a, 'b, T> {
#[inline]
const fn new(underlying: &'b Array2DRef<'a, T>, coord: Coord2D) -> Self {
const fn new(
underlying: &'b OffsetArray2DRef<'a, T>,
coord: Coord2D,
) -> Self {
Self { underlying, coord }
}
}
Expand Down
28 changes: 22 additions & 6 deletions src/std/ndslice/offsetarray2dref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,40 @@ impl<'a, T> OffsetArray2DRef<'a, T> {
#[inline]
#[must_use]
fn get_row(&self, row: RowIndex) -> Option<&'a [T]> {
if row.val() >= self.num_rows().val().get() {
return None;
}
let row =
BoundRowIndex::new(RowCount::new(self.data.num_rows().get()), row)?;
BoundRowIndex::new(RowCount::new(self.data.num_rows().get()), row);
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
let row = unsafe { row.unwrap_unchecked() };
let row = WrappingRowIndex::from(row) + self.origin.row();
self.data.get_row(**row)
let row = self.data.get_row(**row);
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
let row = unsafe { row.unwrap_unchecked() };
Some(row)
}

#[inline]
#[must_use]
pub fn get_elt(&self, index: Coord2D) -> Option<&T> {
let row = self.get_row(index.row())?;
pub fn get_elt(&self, index: Coord2D) -> Option<&'a T> {
let row = self.get_row(RowIndex::new(*index.row()))?;
if *index.col() >= self.row_length().val().get() {
return None;
}

let col = BoundColIndex::new(
RowLength::new(self.data.row_length().get()),
index.col(),
)?;
);
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
let col = unsafe { col.unwrap_unchecked() };
let col = WrappingColIndex::from(col) + self.origin.col();
let col = ***col;
row.get(col)
let col = row.get(col);
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
let col = unsafe { col.unwrap_unchecked() };
Some(col)
}

#[inline]
Expand Down
18 changes: 13 additions & 5 deletions src/std/wrapping_numerics_arith/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ where
(lhs_lo, borrow) = lhs_lo.borrowing_sub(rhs_lo, borrow);
(lhs_hi, borrow) = lhs_hi.borrowing_sub(rhs_hi, borrow);
assert!(!borrow);
assert_eq!(lhs_hi, T::ZERO);
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
unsafe {
core::hint::assert_unchecked(lhs_hi == T::ZERO);
}
lhs_lo
}
}
Expand Down Expand Up @@ -134,13 +137,15 @@ where
}

if let Ok(ibound) = domain.try_into() {
return rhs
let res = rhs
.checked_rem(ibound)
.unwrap()
.checked_add(ibound)
.unwrap()
.try_into()
.unwrap();
.try_into();
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
let res = unsafe { res.unwrap_unchecked() };
return res;
}

rhs.cast_unsigned().wrapping_add(domain)
Expand All @@ -164,7 +169,10 @@ where
let lhs = **self;
let sum = SumAndCarry(lhs.carrying_add(rhs, false));
let rem = sum % **domain;
WrappingUnsigned::new(BoundUnsigned::new(*domain, rem).unwrap())
let res = BoundUnsigned::new(*domain, rem);
#[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
let res = unsafe { res.unwrap_unchecked() };
WrappingUnsigned::new(res)
}
}

Expand Down
Loading