Skip to content

Commit 7f8b039

Browse files
authored
Appease Clippy (#1594)
* use Rc<str> * use strip_prefix
1 parent 88b5e94 commit 7f8b039

3 files changed

Lines changed: 32 additions & 33 deletions

File tree

examples/keyed_list/src/person.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ use yewtil::NeqAssign;
66
#[derive(Clone, Debug, Eq, PartialEq)]
77
pub struct PersonInfo {
88
pub id: usize,
9-
pub name: Rc<String>,
10-
pub address: Rc<String>,
9+
pub name: Rc<str>,
10+
pub address: Rc<str>,
1111
pub age: usize,
1212
}
1313
impl PersonInfo {
1414
pub fn new_random(id: usize) -> Self {
1515
let address = {
1616
let count = random::range_exclusive(3, 6);
17-
Rc::new(random::words(count, 5, 12).join(" "))
17+
Rc::from(random::words(count, 5, 12).join(" ").as_str())
1818
};
1919

2020
Self {
2121
id,
22-
name: Rc::new(random::words(2, 4, 15).join(" ")),
22+
name: Rc::from(random::words(2, 4, 15).join(" ").as_str()),
2323
age: random::range_exclusive(7, 77),
2424
address,
2525
}

yew-router/src/switch.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ pub trait Switch: Sized {
8282
pub struct LeadingSlash<T>(pub T);
8383
impl<U: Switch> Switch for LeadingSlash<U> {
8484
fn from_route_part<STATE>(part: String, state: Option<STATE>) -> (Option<Self>, Option<STATE>) {
85-
if part.starts_with('/') {
86-
let part = part[1..].to_string();
87-
let (inner, state) = U::from_route_part(part, state);
85+
if let Some(part) = part.strip_prefix('/') {
86+
let (inner, state) = U::from_route_part(part.to_owned(), state);
8887
(inner.map(LeadingSlash), state)
8988
} else {
9089
(None, None)

yew/src/virtual_dom/key.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
//! This module contains the implementation yew's virtual nodes' keys.
22
3+
use std::fmt::{self, Display, Formatter};
34
use std::ops::Deref;
45
use std::rc::Rc;
56

67
/// Represents the (optional) key of Yew's virtual nodes.
78
///
89
/// Keys are cheap to clone.
9-
// TODO (#1263): Explain when keys are useful and add an example.
10-
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
10+
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
1111
pub struct Key {
12-
key: Rc<String>,
12+
key: Rc<str>,
1313
}
1414

15-
impl core::fmt::Display for Key {
16-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17-
write!(f, "{}", &self.key)
15+
impl Display for Key {
16+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
17+
self.key.fmt(f)
1818
}
1919
}
2020

21-
impl core::fmt::Debug for Key {
22-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23-
write!(f, "{}", &self.key)
21+
impl Deref for Key {
22+
type Target = str;
23+
24+
fn deref(&self) -> &str {
25+
self.key.as_ref()
2426
}
2527
}
2628

27-
impl From<Rc<String>> for Key {
28-
fn from(key: Rc<String>) -> Self {
29-
Key { key }
29+
impl From<Rc<str>> for Key {
30+
fn from(key: Rc<str>) -> Self {
31+
Self { key }
3032
}
3133
}
3234

33-
impl Deref for Key {
34-
type Target = str;
35-
36-
#[inline]
37-
fn deref(&self) -> &str {
38-
self.key.as_ref()
35+
impl From<&'_ str> for Key {
36+
fn from(key: &'_ str) -> Self {
37+
let key: Rc<str> = Rc::from(key);
38+
Self::from(key)
3939
}
4040
}
4141

4242
macro_rules! key_impl_from_to_string {
4343
($type:ty) => {
4444
impl From<$type> for Key {
4545
fn from(key: $type) -> Self {
46-
Key {
47-
key: Rc::new(key.to_string()),
48-
}
46+
Self::from(key.to_string().as_str())
4947
}
5048
}
5149
};
5250
}
53-
key_impl_from_to_string!(&'static str);
51+
5452
key_impl_from_to_string!(String);
53+
key_impl_from_to_string!(Rc<String>);
5554
key_impl_from_to_string!(char);
56-
key_impl_from_to_string!(usize);
5755
key_impl_from_to_string!(u8);
5856
key_impl_from_to_string!(u16);
5957
key_impl_from_to_string!(u32);
6058
key_impl_from_to_string!(u64);
6159
key_impl_from_to_string!(u128);
60+
key_impl_from_to_string!(usize);
6261
key_impl_from_to_string!(i8);
6362
key_impl_from_to_string!(i16);
6463
key_impl_from_to_string!(i32);
6564
key_impl_from_to_string!(i64);
6665
key_impl_from_to_string!(i128);
66+
key_impl_from_to_string!(isize);
6767

6868
#[cfg(test)]
6969
mod test {
@@ -78,11 +78,11 @@ mod test {
7878

7979
#[test]
8080
fn all_key_conversions() {
81-
let rc_key = Rc::new("rc".to_string());
8281
html! {
8382
<key="string literal">
84-
<img key="String".to_string() />
85-
<p key=rc_key></p>
83+
<img key="String".to_owned() />
84+
<p key=Rc::new("rc".to_owned())></p>
85+
<p key=Rc::<str>::from("rc")></p>
8686
<key='a'>
8787
<p key=11_usize></p>
8888
<p key=12_u8></p>

0 commit comments

Comments
 (0)