Skip to content

Commit eb0fc28

Browse files
Fix doc issue on String and LinearMap
1 parent 303fec8 commit eb0fc28

File tree

6 files changed

+201
-141
lines changed

6 files changed

+201
-141
lines changed

src/defmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Defmt implementations for heapless types
22
33
use crate::{
4-
string::StringInner,
4+
string::{StringInner, StringStorage},
55
vec::{VecInner, VecStorage},
66
};
77
use defmt::Formatter;
@@ -15,7 +15,7 @@ where
1515
}
1616
}
1717

18-
impl<S: VecStorage<u8> + ?Sized> defmt::Format for StringInner<S>
18+
impl<S: StringStorage + ?Sized> defmt::Format for StringInner<S>
1919
where
2020
u8: defmt::Format,
2121
{

src/linear_map.rs

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,103 @@
44
55
use core::{borrow::Borrow, fmt, mem, ops, slice};
66

7-
use crate::vec::{OwnedVecStorage, Vec, VecStorage, ViewVecStorage};
7+
use crate::vec::{OwnedVecStorage, Vec, VecInner, ViewVecStorage};
88

9-
mod sealed {
10-
use crate::vec::{VecInner, VecStorage};
11-
/// Base struct for [`LinearMap`] and [`LinearMapView`]
12-
pub struct LinearMapInnerInner<T, S: VecStorage<T> + ?Sized> {
13-
pub(crate) buffer: VecInner<T, S>,
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 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+
}
1481
}
1582
}
16-
pub(crate) use sealed::LinearMapInnerInner;
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)>;
1789

1890
/// Base struct for [`LinearMap`] and [`LinearMapView`]
19-
pub type LinearMapInner<K, V, S> = LinearMapInnerInner<(K, V), S>;
91+
pub struct LinearMapInner<K, V, S: LinearMapStorage<K, V> + ?Sized> {
92+
pub(crate) buffer: VecInner<(K, V), S>,
93+
}
2094

2195
/// A fixed capacity map/dictionary that performs lookups via linear search.
2296
///
2397
/// Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).
24-
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>>;
2599

26100
/// A dynamic capacity map/dictionary that performs lookups via linear search.
27101
///
28102
/// Note that as this map doesn't use hashing so most operations are *O*(n) instead of *O*(1).
29-
pub type LinearMapView<K, V> = LinearMapInner<K, V, ViewVecStorage<(K, V)>>;
103+
pub type LinearMapView<K, V> = LinearMapInner<K, V, ViewStorage<K, V>>;
30104

31105
impl<K, V, const N: usize> LinearMap<K, V, N> {
32106
/// Creates an empty `LinearMap`.
@@ -47,7 +121,7 @@ impl<K, V, const N: usize> LinearMap<K, V, N> {
47121
}
48122
}
49123

50-
impl<K, V, S: VecStorage<(K, V)> + ?Sized> LinearMapInner<K, V, S>
124+
impl<K, V, S: LinearMapStorage<K, V> + ?Sized> LinearMapInner<K, V, S>
51125
where
52126
K: Eq,
53127
{
@@ -395,7 +469,7 @@ where
395469
}
396470
}
397471

398-
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>
399473
where
400474
K: Borrow<Q> + Eq,
401475
Q: Eq + ?Sized,
@@ -407,7 +481,7 @@ where
407481
}
408482
}
409483

410-
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>
411485
where
412486
K: Borrow<Q> + Eq,
413487
Q: Eq + ?Sized,
@@ -438,7 +512,7 @@ where
438512
}
439513
}
440514

441-
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>
442516
where
443517
K: Eq + fmt::Debug,
444518
V: fmt::Debug,
@@ -496,7 +570,7 @@ where
496570
}
497571
}
498572

499-
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>
500574
where
501575
K: Eq,
502576
{
@@ -543,7 +617,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
543617
}
544618
}
545619

546-
impl<K, V, S1: VecStorage<(K, V)> + ?Sized, S2: VecStorage<(K, V)> + ?Sized>
620+
impl<K, V, S1: LinearMapStorage<K, V> + ?Sized, S2: LinearMapStorage<K, V> + ?Sized>
547621
PartialEq<LinearMapInner<K, V, S2>> for LinearMapInner<K, V, S1>
548622
where
549623
K: Eq,
@@ -557,7 +631,7 @@ where
557631
}
558632
}
559633

560-
impl<K, V, S: VecStorage<(K, V)> + ?Sized> Eq for LinearMapInner<K, V, S>
634+
impl<K, V, S: LinearMapStorage<K, V> + ?Sized> Eq for LinearMapInner<K, V, S>
561635
where
562636
K: Eq,
563637
V: PartialEq,

src/ser.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::{
44
binary_heap::{BinaryHeapInner, Kind as BinaryHeapKind},
55
deque::DequeInner,
66
histbuf::{HistBufStorage, HistoryBufferInner},
7-
linear_map::LinearMapInner,
8-
string::StringInner,
7+
linear_map::{LinearMapInner, LinearMapStorage},
8+
string::{StringInner, StringStorage},
99
vec::{VecInner, VecStorage},
1010
IndexMap, IndexSet,
1111
};
@@ -116,7 +116,7 @@ where
116116
}
117117
}
118118

119-
impl<K, V, S: VecStorage<(K, V)> + ?Sized> Serialize for LinearMapInner<K, V, S>
119+
impl<K, V, S: LinearMapStorage<K, V> + ?Sized> Serialize for LinearMapInner<K, V, S>
120120
where
121121
K: Eq + Serialize,
122122
V: Serialize,
@@ -135,7 +135,7 @@ where
135135

136136
// String containers
137137

138-
impl<S: VecStorage<u8> + ?Sized> Serialize for StringInner<S> {
138+
impl<S: StringStorage + ?Sized> Serialize for StringInner<S> {
139139
fn serialize<SER>(&self, serializer: SER) -> Result<SER::Ok, SER::Error>
140140
where
141141
SER: Serializer,

0 commit comments

Comments
 (0)