Skip to content

Commit e1716d7

Browse files
mangelatsLuthaf
authored andcommitted
Add index traits
1 parent 3bb8fe5 commit e1716d7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,53 @@ pub trait StructOfArray {
168168
type Type;
169169
}
170170

171+
172+
mod private_soa_indexs {
173+
// From [`std::slice::SliceIndex`](https://doc.rust-lang.org/std/slice/trait.SliceIndex.html) code.
174+
// Limits the types that may implement the SoA index traits.
175+
// It's also helpful to have the exaustive list of all accepted types.
176+
177+
use ::std::ops;
178+
179+
pub trait Sealed {}
180+
181+
impl Sealed for usize {} // [a]
182+
impl Sealed for ops::Range<usize> {} // [a..b]
183+
impl Sealed for ops::RangeTo<usize> {} // [..b]
184+
impl Sealed for ops::RangeFrom<usize> {} // [a..]
185+
impl Sealed for ops::RangeFull {} // [..]
186+
impl Sealed for ops::RangeInclusive<usize> {} // [a..=b]
187+
impl Sealed for ops::RangeToInclusive<usize> {} // [..=b]
188+
}
189+
190+
/// Helper trait used for indexing operations.
191+
/// Inspired by [`std::slice::SliceIndex`](https://doc.rust-lang.org/std/slice/trait.SliceIndex.html).
192+
pub trait SoaIndex<T>: private_soa_indexs::Sealed {
193+
/// The output for the non-mutable functions
194+
type RefOutput;
195+
196+
/// Returns the reference output in this location if in bounds. None otherwise.
197+
fn get(self, soa: T) -> Option<Self::RefOutput>;
198+
/// Returns the reference output in this location withotu performing any bounds check.
199+
unsafe fn get_unchecked(self, soa: T) -> Self::RefOutput;
200+
/// Returns the reference output in this location. Panics if it is not in bounds.
201+
fn index(self, soa: T) -> Self::RefOutput;
202+
}
203+
204+
/// Helper trait used for indexing operations returning mutable.
205+
/// Inspired by [`std::slice::SliceIndex`](https://doc.rust-lang.org/std/slice/trait.SliceIndex.html).
206+
pub trait SoaMutIndex<T>: private_soa_indexs::Sealed {
207+
/// The output for the mutable functions
208+
type MutOutput;
209+
210+
/// Returns the mutable reference output in this location if in bounds. None otherwise.
211+
fn get_mut(self, soa: T) -> Option<Self::MutOutput>;
212+
/// Returns the mutable reference output in this location withotu performing any bounds check.
213+
unsafe fn get_unchecked_mut(self, soa: T) -> Self::MutOutput;
214+
/// Returns the mutable reference output in this location. Panics if it is not in bounds.
215+
fn index_mut(self, soa: T) -> Self::MutOutput;
216+
}
217+
171218
/// Create an iterator over multiple fields in a Struct of array style vector.
172219
///
173220
/// This macro takes two main arguments: the array/slice container, and a list

0 commit comments

Comments
 (0)