Skip to main content

bixverse_sparse/
traits.rs

1//! Numeric trait bounds shared by the sparse structures.
2
3use std::ops::{Add, AddAssign, DivAssign, Mul, Sub};
4
5/// More general numeric trait that includes also integers
6pub trait BixverseNumeric:
7    Clone
8    + Default
9    + Copy
10    + Sync
11    + Send
12    + Add<Output = Self>
13    + Mul<Output = Self>
14    + Sub<Output = Self>
15    + AddAssign
16    + PartialEq
17    + Sub
18    + DivAssign
19{
20}
21
22impl<T> BixverseNumeric for T where
23    T: Clone
24        + Default
25        + Copy
26        + Sync
27        + Send
28        + Add<Output = Self>
29        + Mul<Output = Self>
30        + Sub<Output = Self>
31        + AddAssign
32        + PartialEq
33        + Sub
34        + DivAssign
35{
36}
37
38///////////////////
39// Index casting //
40///////////////////
41
42/// Casts index vectors between `usize` and `u32` (memory-width conversion).
43pub trait VecIndexCast<U> {
44    /// Casts the index vector element-wise to the target integer type.
45    fn index_cast(self) -> Vec<U>;
46}
47
48impl VecIndexCast<u32> for Vec<usize> {
49    fn index_cast(self) -> Vec<u32> {
50        self.into_iter().map(|x| x as u32).collect()
51    }
52}
53
54impl VecIndexCast<u32> for &[usize] {
55    fn index_cast(self) -> Vec<u32> {
56        self.iter().map(|&x| x as u32).collect()
57    }
58}
59
60impl VecIndexCast<usize> for Vec<u32> {
61    fn index_cast(self) -> Vec<usize> {
62        self.into_iter().map(|x| x as usize).collect()
63    }
64}
65
66impl VecIndexCast<usize> for &[u32] {
67    fn index_cast(self) -> Vec<usize> {
68        self.iter().map(|&x| x as usize).collect()
69    }
70}