4
4
5
5
use core:: { borrow:: Borrow , fmt, mem, ops, slice} ;
6
6
7
- use crate :: vec:: { OwnedVecStorage , Vec , VecInner , VecStorage , ViewVecStorage } ;
7
+ use crate :: vec:: { OwnedVecStorage , Vec , VecInner , ViewVecStorage } ;
8
+
9
+ mod storage {
10
+ use crate :: vec:: { OwnedVecStorage , VecStorage , ViewVecStorage } ;
11
+
12
+ use super :: { LinearMapInner , LinearMapView } ;
13
+
14
+ /// Trait defining how data for a [`LinearMap`](super::LinearMap) is stored.
15
+ ///
16
+ /// There's two implementations available:
17
+ ///
18
+ /// - [`OwnedStorage`]: stores the data in an array whose size is known at compile time.
19
+ /// - [`ViewStorage`]: stores the data in an unsized slice
20
+ ///
21
+ /// This allows [`LinearMap`] to be generic over either sized or unsized storage. The [`linear_map`](super)
22
+ /// module contains a [`LinearMapInner`] struct that's generic on [`LinearMapStorage`],
23
+ /// and two type aliases for convenience:
24
+ ///
25
+ /// - [`LinearMap<N>`](crate::linear_map::LinearMap) = `LinearMapInner<OwnedStorage<u8, N>>`
26
+ /// - [`LinearMapView<T>`](crate::linear_map::LinearMapView) = `LinearMapInner<ViewStorage<u8>>`
27
+ ///
28
+ /// `LinearMap` can be unsized into `StrinsgView`, either by unsizing coercions such as `&mut LinearMap -> &mut LinearMapView` or
29
+ /// `Box<LinearMap> -> Box<LinearMapView>`, or explicitly with [`.as_view()`](crate::linear_map::LinearMap::as_view) or [`.as_mut_view()`](crate::linear_map::LinearMap::as_mut_view).
30
+ ///
31
+ /// This trait is sealed, so you cannot implement it for your own types. You can only use
32
+ /// the implementations provided by this crate.
33
+ ///
34
+ /// [`LinearMapInner`]: super::LinearMapInner
35
+ /// [`LinearMap`]: super::LinearMap
36
+ /// [`OwnedStorage`]: super::OwnedStorage
37
+ /// [`ViewStorage`]: super::ViewStorage
38
+ pub trait LinearMapStorage < K , V > : LinearMapStorageSealed < K , V > { }
39
+ pub trait LinearMapStorageSealed < K , V > : VecStorage < ( K , V ) > {
40
+ fn as_linear_map_view ( this : & LinearMapInner < K , V , Self > ) -> & LinearMapView < K , V >
41
+ where
42
+ Self : LinearMapStorage < K , V > ;
43
+ fn as_linear_map_mut_view (
44
+ this : & mut LinearMapInner < K , V , Self > ,
45
+ ) -> & mut LinearMapView < K , V >
46
+ where
47
+ Self : LinearMapStorage < K , V > ;
48
+ }
49
+
50
+ impl < K , V , const N : usize > LinearMapStorage < K , V > for OwnedVecStorage < ( K , V ) , N > { }
51
+ impl < K , V , const N : usize > LinearMapStorageSealed < K , V > for OwnedVecStorage < ( K , V ) , N > {
52
+ fn as_linear_map_view ( this : & LinearMapInner < K , V , Self > ) -> & LinearMapView < K , V >
53
+ where
54
+ Self : LinearMapStorage < K , V > ,
55
+ {
56
+ this
57
+ }
58
+ fn as_linear_map_mut_view ( this : & mut LinearMapInner < K , V , Self > ) -> & mut LinearMapView < K , V >
59
+ where
60
+ Self : LinearMapStorage < K , V > ,
61
+ {
62
+ this
63
+ }
64
+ }
65
+
66
+ impl < K , V > LinearMapStorage < K , V > for ViewVecStorage < ( K , V ) > { }
67
+
68
+ impl < K , V > LinearMapStorageSealed < K , V > for ViewVecStorage < ( K , V ) > {
69
+ fn as_linear_map_view ( this : & LinearMapInner < K , V , Self > ) -> & LinearMapView < K , V >
70
+ where
71
+ Self : LinearMapStorage < K , V > ,
72
+ {
73
+ this
74
+ }
75
+ fn as_linear_map_mut_view ( this : & mut LinearMapInner < K , V , Self > ) -> & mut LinearMapView < K , V >
76
+ where
77
+ Self : LinearMapStorage < K , V > ,
78
+ {
79
+ this
80
+ }
81
+ }
82
+ }
83
+
84
+ pub use storage:: LinearMapStorage ;
85
+ /// Implementation of [`LinearMapStorage`] that stores the data in an array whose size is known at compile time.
86
+ pub type OwnedStorage < K , V , const N : usize > = OwnedVecStorage < ( K , V ) , N > ;
87
+ /// Implementation of [`LinearMapStorage`] that stores the data in an unsized slice.
88
+ pub type ViewStorage < K , V > = ViewVecStorage < ( K , V ) > ;
8
89
9
90
/// Base struct for [`LinearMap`] and [`LinearMapView`]
10
- pub struct LinearMapInner < K , V , S : VecStorage < ( K , V ) > + ?Sized > {
91
+ pub struct LinearMapInner < K , V , S : LinearMapStorage < K , V > + ?Sized > {
11
92
pub ( crate ) buffer : VecInner < ( K , V ) , S > ,
12
93
}
13
94
14
95
/// A fixed capacity map/dictionary that performs lookups via linear search.
15
96
///
16
97
/// Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).
17
- pub type LinearMap < K , V , const N : usize > = LinearMapInner < K , V , OwnedVecStorage < ( K , V ) , N > > ;
98
+ pub type LinearMap < K , V , const N : usize > = LinearMapInner < K , V , OwnedStorage < K , V , N > > ;
18
99
19
100
/// A dynamic capacity map/dictionary that performs lookups via linear search.
20
101
///
21
102
/// Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).
22
- pub type LinearMapView < K , V > = LinearMapInner < K , V , ViewVecStorage < ( K , V ) > > ;
103
+ pub type LinearMapView < K , V > = LinearMapInner < K , V , ViewStorage < K , V > > ;
23
104
24
105
impl < K , V , const N : usize > LinearMap < K , V , N > {
25
106
/// Creates an empty `LinearMap`.
@@ -38,22 +119,22 @@ impl<K, V, const N: usize> LinearMap<K, V, N> {
38
119
pub const fn new ( ) -> Self {
39
120
Self { buffer : Vec :: new ( ) }
40
121
}
122
+ }
41
123
124
+ impl < K , V , S : LinearMapStorage < K , V > + ?Sized > LinearMapInner < K , V , S >
125
+ where
126
+ K : Eq ,
127
+ {
42
128
/// Get a reference to the `LinearMap`, erasing the `N` const-generic.
43
129
pub fn as_view ( & self ) -> & LinearMapView < K , V > {
44
- self
130
+ S :: as_linear_map_view ( self )
45
131
}
46
132
47
133
/// Get a mutable reference to the `LinearMap`, erasing the `N` const-generic.
48
134
pub fn as_mut_view ( & mut self ) -> & mut LinearMapView < K , V > {
49
- self
135
+ S :: as_linear_map_mut_view ( self )
50
136
}
51
- }
52
137
53
- impl < K , V , S : VecStorage < ( K , V ) > + ?Sized > LinearMapInner < K , V , S >
54
- where
55
- K : Eq ,
56
- {
57
138
/// Returns the number of elements that the map can hold.
58
139
///
59
140
/// Computes in *O*(1) time.
@@ -388,7 +469,7 @@ where
388
469
}
389
470
}
390
471
391
- impl < K , V , Q , S : VecStorage < ( K , V ) > + ?Sized > ops:: Index < & ' _ Q > for LinearMapInner < K , V , S >
472
+ impl < K , V , Q , S : LinearMapStorage < K , V > + ?Sized > ops:: Index < & ' _ Q > for LinearMapInner < K , V , S >
392
473
where
393
474
K : Borrow < Q > + Eq ,
394
475
Q : Eq + ?Sized ,
@@ -400,7 +481,7 @@ where
400
481
}
401
482
}
402
483
403
- impl < K , V , Q , S : VecStorage < ( K , V ) > + ?Sized > ops:: IndexMut < & ' _ Q > for LinearMapInner < K , V , S >
484
+ impl < K , V , Q , S : LinearMapStorage < K , V > + ?Sized > ops:: IndexMut < & ' _ Q > for LinearMapInner < K , V , S >
404
485
where
405
486
K : Borrow < Q > + Eq ,
406
487
Q : Eq + ?Sized ,
@@ -431,7 +512,7 @@ where
431
512
}
432
513
}
433
514
434
- impl < K , V , S : VecStorage < ( K , V ) > + ?Sized > fmt:: Debug for LinearMapInner < K , V , S >
515
+ impl < K , V , S : LinearMapStorage < K , V > + ?Sized > fmt:: Debug for LinearMapInner < K , V , S >
435
516
where
436
517
K : Eq + fmt:: Debug ,
437
518
V : fmt:: Debug ,
@@ -489,7 +570,7 @@ where
489
570
}
490
571
}
491
572
492
- impl < ' a , K , V , S : VecStorage < ( K , V ) > + ?Sized > IntoIterator for & ' a LinearMapInner < K , V , S >
573
+ impl < ' a , K , V , S : LinearMapStorage < K , V > + ?Sized > IntoIterator for & ' a LinearMapInner < K , V , S >
493
574
where
494
575
K : Eq ,
495
576
{
@@ -533,7 +614,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
533
614
}
534
615
}
535
616
536
- impl < K , V , S1 : VecStorage < ( K , V ) > + ?Sized , S2 : VecStorage < ( K , V ) > + ?Sized >
617
+ impl < K , V , S1 : LinearMapStorage < K , V > + ?Sized , S2 : LinearMapStorage < K , V > + ?Sized >
537
618
PartialEq < LinearMapInner < K , V , S2 > > for LinearMapInner < K , V , S1 >
538
619
where
539
620
K : Eq ,
@@ -547,7 +628,7 @@ where
547
628
}
548
629
}
549
630
550
- impl < K , V , S : VecStorage < ( K , V ) > + ?Sized > Eq for LinearMapInner < K , V , S >
631
+ impl < K , V , S : LinearMapStorage < K , V > + ?Sized > Eq for LinearMapInner < K , V , S >
551
632
where
552
633
K : Eq ,
553
634
V : PartialEq ,
0 commit comments