Skip to content

Commit 0d3961e

Browse files
committed
Move allocator traits to fenris-traits, add bounds for built-ins
1 parent 31d4f4b commit 0d3961e

3 files changed

Lines changed: 92 additions & 56 deletions

File tree

fenris-traits/src/allocators.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//! Helper traits for allocator trait bounds.
2+
use nalgebra::allocator::Allocator;
3+
use nalgebra::{DefaultAllocator, DimName, Scalar, U1};
4+
5+
/// An allocator for a single dimension.
6+
pub trait DimAllocator<T: Scalar, D: DimName>:
7+
Allocator<T, D>
8+
+ Allocator<T, D, D>
9+
+ Allocator<T, U1, D>
10+
// Used for various functionality like decompositions
11+
+ Allocator<usize, D>
12+
+ Allocator<(usize, usize), D>
13+
// Provide allocators for built-in types so that we don't need separate traits to use these
14+
// if we already have Allocator<T, D>
15+
+ Allocator<f32, D>
16+
+ Allocator<f64, D>
17+
+ Allocator<i8, D>
18+
+ Allocator<i32, D>
19+
+ Allocator<i64, D>
20+
+ Allocator<u8, D>
21+
+ Allocator<u16, D>
22+
+ Allocator<u32, D>
23+
+ Allocator<u64, D>
24+
+ Allocator<isize, D>
25+
+ Allocator<bool, D>
26+
{}
27+
28+
impl<T, D> DimAllocator<T, D> for DefaultAllocator
29+
where
30+
T: Scalar,
31+
D: DimName,
32+
DefaultAllocator: Allocator<T, D>
33+
+ Allocator<T, D, D>
34+
+ Allocator<T, U1, D>
35+
+ Allocator<usize, D>
36+
+ Allocator<(usize, usize), D>
37+
+ Allocator<f32, D>
38+
+ Allocator<f64, D>
39+
+ Allocator<i8, D>
40+
+ Allocator<i32, D>
41+
+ Allocator<i64, D>
42+
+ Allocator<u8, D>
43+
+ Allocator<u16, D>
44+
+ Allocator<u32, D>
45+
+ Allocator<u64, D>
46+
+ Allocator<isize, D>
47+
+ Allocator<bool, D>,
48+
{
49+
}
50+
51+
/// An allocator for two dimensions.
52+
pub trait BiDimAllocator<T: Scalar, D1: DimName, D2: DimName>:
53+
DimAllocator<T, D1> + DimAllocator<T, D2> + Allocator<T, D1, D2> + Allocator<T, D2, D1>
54+
{
55+
}
56+
57+
impl<T: Scalar, D1: DimName, D2: DimName> BiDimAllocator<T, D1, D2> for DefaultAllocator where
58+
DefaultAllocator: DimAllocator<T, D1> + DimAllocator<T, D2> + Allocator<T, D1, D2> + Allocator<T, D2, D1>
59+
{
60+
}
61+
62+
/// An allocator for three dimensions.
63+
pub trait TriDimAllocator<T: Scalar, D1: DimName, D2: DimName, D3: DimName>:
64+
BiDimAllocator<T, D1, D2> + BiDimAllocator<T, D1, D3> + BiDimAllocator<T, D2, D3>
65+
{
66+
}
67+
68+
impl<T: Scalar, D1: DimName, D2: DimName, D3: DimName> TriDimAllocator<T, D1, D2, D3> for DefaultAllocator where
69+
DefaultAllocator: BiDimAllocator<T, D1, D2> + BiDimAllocator<T, D1, D3> + BiDimAllocator<T, D2, D3>
70+
{
71+
}
72+
73+
pub trait QuadDimAllocator<T: Scalar, D1: DimName, D2: DimName, D3: DimName, D4: DimName>:
74+
TriDimAllocator<T, D1, D2, D3>
75+
+ TriDimAllocator<T, D1, D2, D4>
76+
+ TriDimAllocator<T, D1, D3, D4>
77+
+ TriDimAllocator<T, D2, D3, D4>
78+
{
79+
}
80+
81+
impl<T: Scalar, D1: DimName, D2: DimName, D3: DimName, D4: DimName> QuadDimAllocator<T, D1, D2, D3, D4>
82+
for DefaultAllocator
83+
where
84+
DefaultAllocator: TriDimAllocator<T, D1, D2, D3>
85+
+ TriDimAllocator<T, D1, D2, D4>
86+
+ TriDimAllocator<T, D1, D3, D4>
87+
+ TriDimAllocator<T, D2, D3, D4>,
88+
{
89+
}

fenris-traits/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ pub use nalgebra;
55
pub trait Real: RealField + Copy {}
66

77
impl<T: RealField + Copy> Real for T {}
8+
9+
pub mod allocators;

src/allocators.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,4 @@ where
2121
{
2222
}
2323

24-
/// An allocator for a single dimension.
25-
pub trait DimAllocator<T: Scalar, D: DimName>:
26-
Allocator<T, D> + Allocator<T, D, D> + Allocator<T, U1, D>
27-
// Used for various functionality like decompositions
28-
+ Allocator<usize, D>
29-
+ Allocator<(usize, usize), D>
30-
{}
31-
32-
impl<T, D> DimAllocator<T, D> for DefaultAllocator
33-
where
34-
T: Scalar,
35-
D: DimName,
36-
DefaultAllocator:
37-
Allocator<T, D> + Allocator<T, D, D> + Allocator<T, U1, D> + Allocator<usize, D> + Allocator<(usize, usize), D>,
38-
{
39-
}
40-
41-
/// An allocator for two dimensions.
42-
pub trait BiDimAllocator<T: Scalar, D1: DimName, D2: DimName>:
43-
DimAllocator<T, D1> + DimAllocator<T, D2> + Allocator<T, D1, D2> + Allocator<T, D2, D1>
44-
{
45-
}
46-
47-
impl<T: Scalar, D1: DimName, D2: DimName> BiDimAllocator<T, D1, D2> for DefaultAllocator where
48-
DefaultAllocator: DimAllocator<T, D1> + DimAllocator<T, D2> + Allocator<T, D1, D2> + Allocator<T, D2, D1>
49-
{
50-
}
51-
52-
/// An allocator for three dimensions.
53-
pub trait TriDimAllocator<T: Scalar, D1: DimName, D2: DimName, D3: DimName>:
54-
BiDimAllocator<T, D1, D2> + BiDimAllocator<T, D1, D3> + BiDimAllocator<T, D2, D3>
55-
{
56-
}
57-
58-
impl<T: Scalar, D1: DimName, D2: DimName, D3: DimName> TriDimAllocator<T, D1, D2, D3> for DefaultAllocator where
59-
DefaultAllocator: BiDimAllocator<T, D1, D2> + BiDimAllocator<T, D1, D3> + BiDimAllocator<T, D2, D3>
60-
{
61-
}
62-
63-
pub trait QuadDimAllocator<T: Scalar, D1: DimName, D2: DimName, D3: DimName, D4: DimName>:
64-
TriDimAllocator<T, D1, D2, D3>
65-
+ TriDimAllocator<T, D1, D2, D4>
66-
+ TriDimAllocator<T, D1, D3, D4>
67-
+ TriDimAllocator<T, D2, D3, D4>
68-
{
69-
}
70-
71-
impl<T: Scalar, D1: DimName, D2: DimName, D3: DimName, D4: DimName> QuadDimAllocator<T, D1, D2, D3, D4>
72-
for DefaultAllocator
73-
where
74-
DefaultAllocator: TriDimAllocator<T, D1, D2, D3>
75-
+ TriDimAllocator<T, D1, D2, D4>
76-
+ TriDimAllocator<T, D1, D3, D4>
77-
+ TriDimAllocator<T, D2, D3, D4>,
78-
{
79-
}
24+
pub use fenris_traits::allocators::*;

0 commit comments

Comments
 (0)