Skip to content

Commit 5bf0b27

Browse files
committed
implements AsRef<str> for easier composition
1 parent 974b9a7 commit 5bf0b27

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/id.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub struct Id<T> {
1818
}
1919

2020
impl<T> Id<T> {
21-
2221
/// private method to build an Id that exists in the [Collection]
2322
fn must_exists(s: String) -> Id<T> {
2423
Id {
@@ -27,12 +26,18 @@ impl<T> Id<T> {
2726
}
2827
}
2928

30-
/// get as str
29+
/// Extracts a string slice containing the entire [Id]
3130
pub fn as_str(&self) -> &str {
3231
self
3332
}
3433
}
3534

35+
impl<T> std::convert::AsRef<str> for Id<T> {
36+
fn as_ref(&self) -> &str {
37+
self
38+
}
39+
}
40+
3641
impl<T> std::ops::Deref for Id<T> {
3742
type Target = str;
3843
fn deref(&self) -> &str {
@@ -58,7 +63,6 @@ impl<T> Default for Collection<T> {
5863
}
5964

6065
impl<T> Collection<T> {
61-
6266
/// Get a typed [Id] from a raw &str
6367
/// An [Id] can be returned only if it exists in the [Collection]
6468
pub fn get_id(&self, raw_id: &str) -> Option<Id<T>> {
@@ -69,10 +73,12 @@ impl<T> Collection<T> {
6973
pub(crate) fn get_by_str(&self, raw_id: &str) -> Option<(&Id<T>, &T)> {
7074
self.0.get_key_value(raw_id)
7175
}
72-
76+
7377
/// Get an &[Id] and a mutable reference to the object associated with it if it exists in the [Collection]
7478
pub(crate) fn get_mut_by_str(&mut self, raw_id: &str) -> Option<(Id<T>, &mut T)> {
75-
self.0.get_mut(raw_id).map(|v| (Id::must_exists(raw_id.to_owned()), v))
79+
self.0
80+
.get_mut(raw_id)
81+
.map(|v| (Id::must_exists(raw_id.to_owned()), v))
7682
}
7783

7884
/// Get the object associated to the typed [Id]

src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn read_pathways() {
184184
let pathways = &gtfs.get_stop("stop1").unwrap().pathways;
185185

186186
assert_eq!(1, pathways.len());
187-
assert_eq!("stop3", pathways[0].to_stop_id.as_str());
187+
assert_eq!("stop3", pathways[0].to_stop_id.as_ref());
188188
assert_eq!(PathwayMode::Walkway, pathways[0].mode);
189189
assert_eq!(
190190
PathwayDirectionType::Unidirectional,

0 commit comments

Comments
 (0)