• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 18683512809

21 Oct 2025 12:16PM UTC coverage: 53.804% (-0.2%) from 54.052%
18683512809

push

github

fasterthanlime
remove fn() layer of indirection where possible

100 of 260 new or added lines in 30 files covered. (38.46%)

22 existing lines in 5 files now uncovered.

4590 of 8531 relevant lines covered (53.8%)

41.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

3.66
/facet-core/src/types/def/ndarray.rs
1
use crate::ptr::{PtrConst, PtrMut};
2

3
use super::Shape;
4

5
/// Fields for n-dimensional array types
6
#[derive(Clone, Copy, Debug)]
7
#[repr(C)]
8
pub struct NdArrayDef {
9
    /// vtable for interacting with the array
10
    pub vtable: &'static NdArrayVTable,
11
    /// shape of the items in the array
12
    pub t: &'static Shape,
13
}
14

15
impl NdArrayDef {
16
    /// Returns a builder for NdArrayDef
17
    pub const fn builder() -> NdArrayDefBuilder {
×
18
        NdArrayDefBuilder::new()
×
19
    }
×
20

21
    /// Returns the shape of the items in the array
22
    pub const fn t(&self) -> &'static Shape {
8✔
23
        self.t
8✔
24
    }
8✔
25
}
26

27
/// Builder for NdArrayDef
28
pub struct NdArrayDefBuilder {
29
    vtable: Option<&'static NdArrayVTable>,
30
    t: Option<&'static Shape>,
31
}
32

33
impl NdArrayDefBuilder {
34
    /// Creates a new NdArrayDefBuilder
35
    #[allow(clippy::new_without_default)]
36
    pub const fn new() -> Self {
×
37
        Self {
×
38
            vtable: None,
×
39
            t: None,
×
40
        }
×
41
    }
×
42

43
    /// Sets the vtable for the NdArrayDef
44
    pub const fn vtable(mut self, vtable: &'static NdArrayVTable) -> Self {
×
45
        self.vtable = Some(vtable);
×
46
        self
×
47
    }
×
48

49
    /// Sets the item shape for the NdArrayDef
NEW
50
    pub const fn t(mut self, t: &'static Shape) -> Self {
×
51
        self.t = Some(t);
×
52
        self
×
53
    }
×
54

55
    /// Builds the NdArrayDef
56
    pub const fn build(self) -> NdArrayDef {
×
57
        NdArrayDef {
×
58
            vtable: self.vtable.unwrap(),
×
59
            t: self.t.unwrap(),
×
60
        }
×
61
    }
×
62
}
63

64
/// Get the total count of elements in the array.
65
///
66
/// # Safety
67
///
68
/// The `array` parameter must point to aligned, initialized memory of the correct type.
69
pub type NdArrayCountFn = unsafe fn(array: PtrConst) -> usize;
70

71
/// Get the number of dimensions in the array.
72
///
73
/// # Safety
74
///
75
/// The `array` parameter must point to aligned, initialized memory of the correct type.
76
pub type NdArrayNDimFn = unsafe fn(array: PtrConst) -> usize;
77

78
/// Get the i-th dimension in the array, or `None` if the dimension index is out of bounds.
79
///
80
/// # Safety
81
///
82
/// The `array` parameter must point to aligned, initialized memory of the correct type.
83
pub type NdArrayDimFn = unsafe fn(array: PtrConst, i: usize) -> Option<usize>;
84

85
/// Get the i-th stride in the array in bytes, or `None` if the dimension index is out of bounds.
86
///
87
/// # Safety
88
///
89
/// The `array` parameter must point to aligned, initialized memory of the correct type.
90
pub type NdArrayByteStrideFn = unsafe fn(array: PtrConst, i: usize) -> Option<isize>;
91

92
/// Get pointer to the element at `index` in the array, or `None` if the
93
/// index is out of bounds.
94
///
95
/// The flat index is transformed into separate array indices like this:
96
/// ```text
97
///  - i0 = index % d0;
98
///  - i1 = index / d0 % d1;
99
///  - …
100
///  - i{n-1} = index / d0 / d1 / ... / d{n-1} % dn;
101
///  - remainder = index / d0 / d1 / ... / dn;
102
/// ```
103
///
104
/// if `remainder` is non-zero, the index is out of bounds and `None` is returned.
105
///
106
/// # Safety
107
///
108
/// The `array` parameter must point to aligned, initialized memory of the correct type.
109
pub type NdArrayGetFn = unsafe fn(array: PtrConst, index: usize) -> Option<PtrConst>;
110

111
/// Get mutable pointer to the element at `index` in the array, or `None` if the
112
/// index is out of bounds.
113
///
114
/// # Safety
115
///
116
/// The `array` parameter must point to aligned, initialized memory of the correct type.
117
pub type NdArrayGetMutFn = unsafe fn(array: PtrMut, index: usize) -> Option<PtrMut>;
118

119
/// Get pointer to the data buffer of the array.
120
///
121
/// # Safety
122
///
123
/// The `array` parameter must point to aligned, initialized memory of the correct type.
124
pub type NdArrayAsPtrFn = unsafe fn(array: PtrConst) -> PtrConst;
125

126
/// Get mutable pointer to the data buffer of the array.
127
///
128
/// # Safety
129
///
130
/// The `array` parameter must point to aligned, initialized memory of the correct type.
131
pub type NdArrayAsMutPtrFn = unsafe fn(array: PtrMut) -> PtrMut;
132

133
/// Virtual table for a n-dimensional array type (like `Matrix<T>`, `Tensor<T>`, etc.)
134
#[derive(Clone, Copy, Debug)]
135
#[repr(C)]
136
pub struct NdArrayVTable {
137
    /// cf. [`NdArrayCountFn`]
138
    pub count: NdArrayCountFn,
139

140
    /// cf. [`NdArrayNDimFn`]
141
    pub n_dim: NdArrayNDimFn,
142

143
    /// cf. [`NdArrayDimFn`]
144
    pub dim: NdArrayDimFn,
145

146
    /// cf. [`NdArrayGetFn`]
147
    pub get: NdArrayGetFn,
148

149
    /// cf. [`NdArrayGetMutFn`]
150
    /// Only available for mutable arrays
151
    pub get_mut: Option<NdArrayGetMutFn>,
152

153
    /// cf. [`NdArrayByteStrideFn`]
154
    /// Only available for types that can be accessed as a strided array
155
    pub byte_stride: Option<NdArrayByteStrideFn>,
156

157
    /// cf. [`NdArrayAsPtrFn`]
158
    /// Only available for types that can be accessed as a strided array
159
    pub as_ptr: Option<NdArrayAsPtrFn>,
160

161
    /// cf. [`NdArrayAsMutPtrFn`]
162
    /// Only available for types that can be accessed as a strided array
163
    pub as_mut_ptr: Option<NdArrayAsMutPtrFn>,
164
}
165

166
impl NdArrayVTable {
167
    /// Returns a builder for NdArrayVTable
168
    pub const fn builder() -> NdArrayVTableBuilder {
×
169
        NdArrayVTableBuilder::new()
×
170
    }
×
171
}
172

173
/// Builds a [`NdArrayVTable`]
174
pub struct NdArrayVTableBuilder {
175
    count: Option<NdArrayCountFn>,
176
    n_dim: Option<NdArrayNDimFn>,
177
    dim: Option<NdArrayDimFn>,
178
    get: Option<NdArrayGetFn>,
179
    get_mut: Option<NdArrayGetMutFn>,
180
    byte_stride: Option<NdArrayByteStrideFn>,
181
    as_ptr: Option<NdArrayAsPtrFn>,
182
    as_mut_ptr: Option<NdArrayAsMutPtrFn>,
183
}
184

185
impl NdArrayVTableBuilder {
186
    /// Creates a new [`NdArrayVTableBuilder`] with all fields set to `None`.
187
    #[allow(clippy::new_without_default)]
188
    pub const fn new() -> Self {
×
189
        Self {
×
190
            count: None,
×
191
            n_dim: None,
×
192
            dim: None,
×
193
            get: None,
×
194
            get_mut: None,
×
195
            byte_stride: None,
×
196
            as_ptr: None,
×
197
            as_mut_ptr: None,
×
198
        }
×
199
    }
×
200

201
    /// Sets the `n_dim` field
202
    pub const fn n_dim(mut self, f: NdArrayNDimFn) -> Self {
×
203
        self.n_dim = Some(f);
×
204
        self
×
205
    }
×
206

207
    /// Sets the `dim` field
208
    pub const fn dim(mut self, f: NdArrayDimFn) -> Self {
×
209
        self.dim = Some(f);
×
210
        self
×
211
    }
×
212

213
    /// Sets the `get` field
214
    pub const fn get(mut self, f: NdArrayGetFn) -> Self {
×
215
        self.get = Some(f);
×
216
        self
×
217
    }
×
218

219
    /// Sets the `get_mut` field
220
    pub const fn get_mut(mut self, f: NdArrayGetMutFn) -> Self {
×
221
        self.get_mut = Some(f);
×
222
        self
×
223
    }
×
224

225
    /// Sets the `byte_stride` field
226
    pub const fn byte_stride(mut self, f: NdArrayByteStrideFn) -> Self {
×
227
        self.byte_stride = Some(f);
×
228
        self
×
229
    }
×
230

231
    /// Sets the `as_ptr` field
232
    pub const fn as_ptr(mut self, f: NdArrayAsPtrFn) -> Self {
×
233
        self.as_ptr = Some(f);
×
234
        self
×
235
    }
×
236

237
    /// Sets the `as_mut_ptr` field
238
    pub const fn as_mut_ptr(mut self, f: NdArrayAsMutPtrFn) -> Self {
×
239
        self.as_mut_ptr = Some(f);
×
240
        self
×
241
    }
×
242

243
    /// Builds the [`NdArrayVTable`] from the current state of the builder.
244
    ///
245
    /// # Panics
246
    ///
247
    /// Panic if any of the required fields (len, get, as_ptr, iter_vtable) are `None`.
248
    pub const fn build(self) -> NdArrayVTable {
×
249
        assert!(self.as_ptr.is_some());
×
250
        NdArrayVTable {
×
251
            count: self.count.unwrap(),
×
252
            n_dim: self.n_dim.unwrap(),
×
253
            dim: self.dim.unwrap(),
×
254
            get: self.get.unwrap(),
×
255
            get_mut: self.get_mut,
×
256
            byte_stride: self.byte_stride,
×
257
            as_ptr: self.as_ptr,
×
258
            as_mut_ptr: self.as_mut_ptr,
×
259
        }
×
260
    }
×
261
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc