Coverage Report

Created: 2026-11-12 08:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/imgref-1.12.3/src/ops.rs
Line
Count
Source
1
use super::Img;
2
use alloc::vec::Vec;
3
use core::ops;
4
5
#[cfg(test)]
6
use alloc::vec;
7
8
macro_rules! impl_imgref_index {
9
    ($container:ty, $index:ty) => {
10
        impl<'a, Pixel: Copy> ops::Index<($index, $index)> for Img<$container> {
11
            type Output = Pixel;
12
13
            /// Read a pixel at `(x,y)` location (e.g. px = `img[(x,y)]`)
14
            ///
15
            /// Coordinates may be outside `width`/`height` if the buffer has enough padding.
16
            /// The x coordinate can't exceed `stride`.
17
            #[inline(always)]
18
            #[cfg_attr(debug_assertions, track_caller)]
19
0
            fn index(&self, (x, y): ($index, $index)) -> &Self::Output {
20
0
                let stride = self.stride();
21
0
                debug_assert_eq!(stride, stride as $index as usize);
22
0
                debug_assert!(x < stride as $index);
23
0
                let x = x as usize;
24
0
                let y = y as usize;
25
0
                &self.buf()[y * stride + x]
26
0
            }
Unexecuted instantiation: <imgref::Img<&[_]> as core::ops::index::Index<(usize, usize)>>::index
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::Index<(usize, usize)>>::index
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::Index<(u32, u32)>>::index
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::Index<(usize, usize)>>::index
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::Index<(u32, u32)>>::index
Unexecuted instantiation: <imgref::Img<&[_]> as core::ops::index::Index<(u32, u32)>>::index
27
        }
28
    };
29
}
30
31
macro_rules! impl_imgref_index_mut {
32
    ($container:ty, $index:ty) => {
33
        impl<'a, Pixel: Copy> ops::IndexMut<($index, $index)> for Img<$container> {
34
            /// Write a pixel at `(x,y)` location (e.g. `img[(x,y)] = px`)
35
            ///
36
            /// Coordinates may be outside `width`/`height` if the buffer has enough padding.
37
            /// The x coordinate can't exceed `stride`.
38
            #[inline(always)]
39
            #[cfg_attr(debug_assertions, track_caller)]
40
0
            fn index_mut(&mut self, (x, y): ($index, $index)) -> &mut Self::Output {
41
0
                let stride = self.stride();
42
0
                debug_assert_eq!(stride, stride as $index as usize);
43
0
                debug_assert!(x < stride as $index);
44
0
                let x = x as usize;
45
0
                let y = y as usize;
46
0
                &mut self.buf_mut()[y * stride + x]
47
0
            }
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::IndexMut<(usize, usize)>>::index_mut
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::IndexMut<(u32, u32)>>::index_mut
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::IndexMut<(usize, usize)>>::index_mut
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::IndexMut<(u32, u32)>>::index_mut
48
        }
49
    };
50
}
51
52
impl_imgref_index! {&'a [Pixel], usize}
53
impl_imgref_index! {&'a [Pixel], u32}
54
impl_imgref_index! {&'a mut [Pixel], usize}
55
impl_imgref_index! {&'a mut [Pixel], u32}
56
impl_imgref_index_mut! {&'a mut [Pixel], usize}
57
impl_imgref_index_mut! {&'a mut [Pixel], u32}
58
impl_imgref_index! {Vec<Pixel>, usize}
59
impl_imgref_index! {Vec<Pixel>, u32}
60
impl_imgref_index_mut! {Vec<Pixel>, usize}
61
impl_imgref_index_mut! {Vec<Pixel>, u32}
62
63
#[test]
64
fn index() {
65
    let mut img = Img::new_stride(vec![1,2,3,4,5,6,7,8], 2, 2, 3);
66
    assert_eq!(1, img[(0u32,0u32)]);
67
    assert_eq!(2, img.as_ref()[(1usize,0usize)]);
68
    assert_eq!(3, img.as_ref()[(2u32,0u32)]);
69
    assert_eq!(4, img[(0usize,1usize)]);
70
    assert_eq!(8, img[(1usize,2usize)]);
71
    assert_eq!(5, img.sub_image_mut(1,1,1,1)[(0usize,0usize)]);
72
}
73
74
#[test]
75
#[should_panic]
76
fn index_u32_with_huge_stride_does_not_wrap() {
77
    let img = Img::new_stride(vec![1, 2, 3], 1, 1, 1usize << 32);
78
    let _ = img[(0u32, 1u32)];
79
}
80
81
macro_rules! impl_imgref_row_index {
82
    ($container:ty) => {
83
        impl<'a, Pixel: Copy> ops::Index<usize> for Img<$container> {
84
            type Output = [Pixel];
85
86
            #[inline(always)]
87
            /// Take n-th row as a slice. Same as `.rows().nth(n).unwrap()`
88
            ///
89
            /// Slice length is guaranteed to equal image width.
90
            /// Row must be within image height.
91
0
            fn index(&self, row: usize) -> &Self::Output {
92
0
                let stride = self.stride();
93
0
                let width = self.width();
94
0
                let start = row * stride;
95
0
                self.buf().get(start .. start + width).unwrap_or_else(|| index_fail(row))
Unexecuted instantiation: <imgref::Img<&[_]> as core::ops::index::Index<usize>>::index::{closure#0}
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::Index<usize>>::index::{closure#0}
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::Index<usize>>::index::{closure#0}
96
0
            }
Unexecuted instantiation: <imgref::Img<&[_]> as core::ops::index::Index<usize>>::index
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::Index<usize>>::index
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::Index<usize>>::index
97
        }
98
    };
99
}
100
101
macro_rules! impl_imgref_row_index_mut {
102
    ($container:ty) => {
103
        impl<'a, Pixel: Copy> ops::IndexMut<usize> for Img<$container> {
104
            #[inline(always)]
105
            /// Take n-th row as a mutable slice. Same as `.rows().nth(n).unwrap()`
106
            ///
107
            /// Slice length is guaranteed to equal image width.
108
            /// Row must be within image height.
109
0
            fn index_mut(&mut self, row: usize) -> &mut Self::Output {
110
0
                let stride = self.stride();
111
0
                let width = self.width();
112
0
                let start = row * stride;
113
0
                self.buf_mut().get_mut(start .. start + width).unwrap_or_else(|| index_fail(row))
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::IndexMut<usize>>::index_mut::{closure#0}
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::IndexMut<usize>>::index_mut::{closure#0}
114
0
            }
Unexecuted instantiation: <imgref::Img<&mut [_]> as core::ops::index::IndexMut<usize>>::index_mut
Unexecuted instantiation: <imgref::Img<alloc::vec::Vec<_>> as core::ops::index::IndexMut<usize>>::index_mut
115
        }
116
    };
117
}
118
119
impl_imgref_row_index! {&'a [Pixel]}
120
impl_imgref_row_index! {&'a mut [Pixel]}
121
impl_imgref_row_index_mut! {&'a mut [Pixel]}
122
impl_imgref_row_index! {Vec<Pixel>}
123
impl_imgref_row_index_mut! {Vec<Pixel>}
124
125
#[cold]
126
0
fn index_fail(row: usize) -> ! {
127
0
    panic!("row {row} is out of range")
128
}
129
130
#[test]
131
fn index_by_row() {
132
    let mut img = Img::new_stride(vec![1,2,3,4,5,6,7,8], 2, 2, 3);
133
    assert_eq!(&[1,2], &img[0]);
134
    assert_eq!(&[4,5], &img[1]);
135
    assert_eq!(&[1,2], &img.as_ref()[0]);
136
    assert_eq!(&[4,5], &img.as_ref()[1]);
137
    assert_eq!(&[1,2], &img.as_mut()[0]);
138
    assert_eq!(&[4,5], &img.as_mut()[1]);
139
}