Skip to content

Commit b70bf54

Browse files
committed
separate type bounds and apply clippy lint fixes
1 parent 0cf0502 commit b70bf54

File tree

5 files changed

+143
-106
lines changed

5 files changed

+143
-106
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ structdiff-derive = { path = "derive", version = "=0.7.1" }
2828
bincode = "1.3.3"
2929
assert_unordered = "0.3.5"
3030
nanorand = { version = "0.7.0" }
31+
32+
[lints.rust]
33+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(unused)'] }

derive/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ serde = { version = "^1.0.0", optional = true, features = ["derive
1919
"serde" = ["dep:serde"]
2020
"debug_diffs" = []
2121
"generated_setters" = []
22+
23+
[lints.rust]
24+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(unused)'] }

src/collections/ordered_array_like.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub fn hirschberg<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
2424
source_end: source.len(),
2525
},
2626
)
27-
.into_iter()
2827
.collect::<Vec<_>>()
2928
{
3029
empty if empty.is_empty() => None,
@@ -52,7 +51,6 @@ pub fn levenshtein<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
5251
source_end: source.len(),
5352
},
5453
)
55-
.into_iter()
5654
.collect::<Vec<_>>()
5755
{
5856
empty if empty.is_empty() => None,
@@ -314,8 +312,7 @@ fn hirschberg_impl<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
314312
(false, true) => {
315313
let iter: Box<dyn Iterator<Item = _>> = Box::new(
316314
target[target_start..target_end]
317-
.into_iter()
318-
.map(|a| *a)
315+
.iter().copied()
319316
.enumerate()
320317
.map(|(i, v)| {
321318
let idx = source_end + i;
@@ -367,8 +364,8 @@ fn hirschberg_impl<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
367364
.unwrap();
368365

369366
let left = hirschberg_impl(
370-
&target,
371-
&source,
367+
target,
368+
source,
372369
Indices {
373370
target_end: target_split_index,
374371
source_end: source_split_index,
@@ -377,8 +374,8 @@ fn hirschberg_impl<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
377374
);
378375

379376
let right = hirschberg_impl(
380-
&target,
381-
&source,
377+
target,
378+
source,
382379
Indices {
383380
target_start: target_split_index,
384381
source_start: source_split_index,
@@ -561,8 +558,8 @@ fn levenshtein_impl<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
561558

562559
changelist_from_change_table(
563560
table,
564-
&target,
565-
&source,
561+
target,
562+
source,
566563
Indices {
567564
target_start,
568565
target_end,

src/collections/rope.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ impl From<usize> for Key {
8282
}
8383
}
8484

85-
impl Into<usize> for Key {
86-
fn into(self) -> usize {
87-
self.0.load(Relaxed)
85+
impl From<Key> for usize {
86+
fn from(val: Key) -> Self {
87+
val.0.load(Relaxed)
8888
}
8989
}
9090

91-
impl Into<usize> for &Key {
92-
fn into(self) -> usize {
93-
self.0.load(Relaxed)
91+
impl From<&Key> for usize {
92+
fn from(val: &Key) -> Self {
93+
val.0.load(Relaxed)
9494
}
9595
}
9696

@@ -127,8 +127,7 @@ impl<T> Index<usize> for Rope<T> {
127127
.iter()
128128
.skip_while(|(k, content)| Into::<usize>::into(*k) + content.len() < index + 1)
129129
.next()
130-
.map(|(k, content)| content.get(index - Into::<usize>::into(k)))
131-
.flatten()
130+
.and_then(|(k, content)| content.get(index - Into::<usize>::into(k)))
132131
.unwrap()
133132
}
134133
}
@@ -139,8 +138,7 @@ impl<T> IndexMut<usize> for Rope<T> {
139138
.iter_mut()
140139
.skip_while(|(k, content)| Into::<usize>::into(*k) + content.len() < index + 1)
141140
.next()
142-
.map(|(k, content)| content.get_mut(index - Into::<usize>::into(k)))
143-
.flatten()
141+
.and_then(|(k, content)| content.get_mut(index - Into::<usize>::into(k)))
144142
.unwrap()
145143
}
146144
}
@@ -173,7 +171,7 @@ impl<'rope, T: 'rope> Iterator for Iter<'rope, T> {
173171
.unwrap_or_default();
174172

175173
while new_in_key < max_in_slot {
176-
if let Some(_) = self.self_ref.0.get(key).and_then(|v| v.get(new_in_key)) {
174+
if self.self_ref.0.get(key).and_then(|v| v.get(new_in_key)).is_some() {
177175
self.key = Into::<usize>::into(key);
178176
self.in_key = new_in_key;
179177
return ret;
@@ -238,11 +236,11 @@ impl<'rope, T: 'rope> IntoIterator for &'rope Rope<T> {
238236

239237
impl<T> FromIterator<T> for Rope<T> {
240238
fn from_iter<C: IntoIterator<Item = T>>(iter: C) -> Self {
241-
let mut iter = iter.into_iter();
239+
let iter = iter.into_iter();
242240
let mut counter = 0;
243241
let mut current = VecDeque::with_capacity(MAX_SLOT_SIZE);
244242
let mut map = BTreeMap::new();
245-
while let Some(item) = iter.next() {
243+
for item in iter {
246244
current.push_back(item);
247245
counter += 1;
248246
if counter % DEF_SLOT_SIZE == 0 {
@@ -261,6 +259,12 @@ impl<T> FromIterator<T> for Rope<T> {
261259
}
262260
}
263261

262+
impl<T> Default for Rope<T> {
263+
fn default() -> Self {
264+
Self::new()
265+
}
266+
}
267+
264268
impl<T> Rope<T> {
265269
pub fn new() -> Self {
266270
Self(BTreeMap::from([(
@@ -308,9 +312,7 @@ impl<T> Rope<T> {
308312
let key = self.key_for_index(from);
309313
let prev_high_index = self
310314
.0
311-
.range(..key)
312-
.rev()
313-
.next()
315+
.range(..key).next_back()
314316
.map(|(k, _)| k.clone())
315317
.unwrap_or_default();
316318
let keys: Vec<Key> = self
@@ -323,7 +325,7 @@ impl<T> Rope<T> {
323325
let mut hold = VecDeque::<T>::with_capacity(0);
324326

325327
for key in keys.iter() {
326-
let entry = self.0.get_mut(&key).unwrap();
328+
let entry = self.0.get_mut(key).unwrap();
327329
if entry.is_empty() {
328330
continue;
329331
}
@@ -358,7 +360,7 @@ impl<T> Rope<T> {
358360
}
359361

360362
// take the empty holder back and leave the values in the map entry
361-
std::mem::swap(self.0.get_mut(&key).unwrap(), &mut hold);
363+
std::mem::swap(self.0.get_mut(key).unwrap(), &mut hold);
362364
}
363365

364366
self.0.retain(|_, v| !v.is_empty());

src/lib.rs

Lines changed: 110 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,124 @@ pub use structdiff_derive::Difference;
88

99
pub mod collections;
1010

11+
#[cfg(all(feature = "nanoserde", feature = "serde", feature = "debug_diffs"))]
12+
pub(crate) mod __private {
13+
use super::*;
14+
pub trait StructDiffOwnedBound:
15+
SerBin + DeBin + Serialize + DeserializeOwned + Clone + std::fmt::Debug
16+
{
17+
}
18+
impl<T: SerBin + DeBin + Serialize + DeserializeOwned + Clone + std::fmt::Debug>
19+
StructDiffOwnedBound for T
20+
{
21+
}
22+
23+
pub trait StructDiffRefBound: SerBin + Serialize + Clone + std::fmt::Debug {}
24+
impl<T: SerBin + Serialize + Clone + std::fmt::Debug> StructDiffRefBound for T {}
25+
}
26+
27+
#[cfg(all(feature = "nanoserde", not(feature = "serde"), feature = "debug_diffs"))]
28+
pub(crate) mod __private {
29+
use super::*;
30+
31+
pub trait StructDiffOwnedBound: SerBin + DeBin + Clone + std::fmt::Debug {}
32+
impl<T: SerBin + DeBin + Clone + std::fmt::Debug> StructDiffOwnedBound for T {}
33+
34+
pub trait StructDiffRefBound: SerBin + Clone + std::fmt::Debug {}
35+
impl<T: SerBin + Clone + std::fmt::Debug> StructDiffRefBound for T {}
36+
}
37+
38+
#[cfg(all(feature = "serde", not(feature = "nanoserde"), feature = "debug_diffs"))]
39+
pub(crate) mod __private {
40+
use super::*;
41+
42+
pub trait StructDiffOwnedBound: Serialize + DeserializeOwned + Clone + std::fmt::Debug {}
43+
impl<T: Serialize + DeserializeOwned + Clone + std::fmt::Debug> StructDiffOwnedBound for T {}
44+
45+
pub trait StructDiffRefBound: Serialize + Clone + std::fmt::Debug {}
46+
impl<T: Serialize + Clone + std::fmt::Debug> StructDiffRefBound for T {}
47+
}
48+
49+
#[cfg(all(
50+
not(feature = "serde"),
51+
not(feature = "nanoserde"),
52+
feature = "debug_diffs"
53+
))]
54+
pub(crate) mod __private {
55+
use super::*;
56+
57+
pub trait StructDiffOwnedBound: Clone + std::fmt::Debug {}
58+
impl<T: Clone + std::fmt::Debug> StructDiffOwnedBound for T {}
59+
60+
pub trait StructDiffRefBound: Clone + std::fmt::Debug {}
61+
impl<T: Clone + std::fmt::Debug> StructDiffRefBound for T {}
62+
}
63+
64+
#[cfg(all(feature = "nanoserde", feature = "serde", not(feature = "debug_diffs")))]
65+
pub(crate) mod __private {
66+
use super::*;
67+
pub trait StructDiffOwnedBound: SerBin + DeBin + Serialize + DeserializeOwned + Clone {}
68+
impl<T: SerBin + DeBin + Serialize + DeserializeOwned + Clone> StructDiffOwnedBound for T {}
69+
70+
pub trait StructDiffRefBound: SerBin + Serialize + Clone {}
71+
impl<T: SerBin + Serialize + Clone> StructDiffRefBound for T {}
72+
}
73+
74+
#[cfg(all(
75+
feature = "nanoserde",
76+
not(feature = "serde"),
77+
not(feature = "debug_diffs")
78+
))]
79+
pub(crate) mod __private {
80+
use super::*;
81+
82+
pub trait StructDiffOwnedBound: SerBin + DeBin + Clone {}
83+
impl<T: SerBin + DeBin + Clone> StructDiffOwnedBound for T {}
84+
85+
pub trait StructDiffRefBound: SerBin + Clone {}
86+
impl<T: SerBin + Clone> StructDiffRefBound for T {}
87+
}
88+
89+
#[cfg(all(
90+
feature = "serde",
91+
not(feature = "nanoserde"),
92+
not(feature = "debug_diffs")
93+
))]
94+
pub(crate) mod __private {
95+
use super::*;
96+
97+
pub trait StructDiffOwnedBound: Serialize + DeserializeOwned + Clone {}
98+
impl<T: Serialize + DeserializeOwned + Clone> StructDiffOwnedBound for T {}
99+
100+
pub trait StructDiffRefBound: Serialize + Clone {}
101+
impl<T: Serialize + Clone> StructDiffRefBound for T {}
102+
}
103+
104+
#[cfg(all(
105+
not(feature = "serde"),
106+
not(feature = "nanoserde"),
107+
not(feature = "debug_diffs")
108+
))]
109+
pub(crate) mod __private {
110+
111+
112+
pub trait StructDiffOwnedBound: Clone {}
113+
impl<T: Clone> StructDiffOwnedBound for T {}
114+
115+
pub trait StructDiffRefBound: Clone {}
116+
impl<T: Clone> StructDiffRefBound for T {}
117+
}
118+
11119
pub trait StructDiff {
12120
/// A generated type used to represent the difference
13121
/// between two instances of a struct which implements
14122
/// the StructDiff trait.
15-
#[cfg(all(feature = "nanoserde", feature = "serde", feature = "debug_diffs"))]
16-
type Diff: SerBin + DeBin + Serialize + DeserializeOwned + Clone + std::fmt::Debug;
17-
#[cfg(all(feature = "nanoserde", not(feature = "serde"), feature = "debug_diffs"))]
18-
type Diff: SerBin + DeBin + Clone + std::fmt::Debug;
19-
#[cfg(all(feature = "serde", not(feature = "nanoserde"), feature = "debug_diffs"))]
20-
type Diff: Serialize + DeserializeOwned + Clone + std::fmt::Debug;
21-
#[cfg(all(
22-
not(feature = "serde"),
23-
not(feature = "nanoserde"),
24-
feature = "debug_diffs"
25-
))]
26-
type Diff: Clone + std::fmt::Debug;
27-
#[cfg(all(feature = "nanoserde", feature = "serde", not(feature = "debug_diffs")))]
28-
type Diff: SerBin + DeBin + Serialize + DeserializeOwned + Clone;
29-
#[cfg(all(
30-
feature = "nanoserde",
31-
not(feature = "serde"),
32-
not(feature = "debug_diffs")
33-
))]
34-
type Diff: SerBin + DeBin + Clone;
35-
#[cfg(all(
36-
feature = "serde",
37-
not(feature = "nanoserde"),
38-
not(feature = "debug_diffs")
39-
))]
40-
type Diff: Serialize + DeserializeOwned + Clone;
41-
#[cfg(all(
42-
not(feature = "serde"),
43-
not(feature = "nanoserde"),
44-
not(feature = "debug_diffs")
45-
))]
46-
type Diff: Clone;
123+
type Diff: __private::StructDiffOwnedBound;
47124

48125
/// A generated type used to represent the difference
49126
/// between two instances of a struct which implements
50127
/// the StructDiff trait (using references).
51-
#[cfg(all(feature = "nanoserde", feature = "serde", feature = "debug_diffs"))]
52-
type DiffRef<'target>: SerBin + Serialize + Clone + std::fmt::Debug + Into<Self::Diff>
53-
where
54-
Self: 'target;
55-
#[cfg(all(feature = "nanoserde", not(feature = "serde"), feature = "debug_diffs"))]
56-
type DiffRef<'target>: SerBin + Clone + std::fmt::Debug + Into<Self::Diff>
57-
where
58-
Self: 'target;
59-
#[cfg(all(feature = "serde", not(feature = "nanoserde"), feature = "debug_diffs"))]
60-
type DiffRef<'target>: Serialize + Clone + std::fmt::Debug + Into<Self::Diff>
61-
where
62-
Self: 'target;
63-
#[cfg(all(
64-
not(feature = "serde"),
65-
not(feature = "nanoserde"),
66-
feature = "debug_diffs"
67-
))]
68-
type DiffRef<'target>: Clone + std::fmt::Debug + Into<Self::Diff>
69-
where
70-
Self: 'target;
71-
#[cfg(all(feature = "nanoserde", feature = "serde", not(feature = "debug_diffs")))]
72-
type DiffRef<'target>: SerBin + Serialize + Clone + Into<Self::Diff>
73-
where
74-
Self: 'target;
75-
#[cfg(all(
76-
feature = "nanoserde",
77-
not(feature = "serde"),
78-
not(feature = "debug_diffs")
79-
))]
80-
type DiffRef<'target>: SerBin + Clone + Into<Self::Diff>
81-
where
82-
Self: 'target;
83-
#[cfg(all(
84-
feature = "serde",
85-
not(feature = "nanoserde"),
86-
not(feature = "debug_diffs")
87-
))]
88-
type DiffRef<'target>: Serialize + Clone + Into<Self::Diff>
89-
where
90-
Self: 'target;
91-
#[cfg(all(
92-
not(feature = "serde"),
93-
not(feature = "nanoserde"),
94-
not(feature = "debug_diffs")
95-
))]
96-
type DiffRef<'target>: Clone + Into<Self::Diff>
128+
type DiffRef<'target>: __private::StructDiffRefBound + Into<Self::Diff>
97129
where
98130
Self: 'target;
99131

0 commit comments

Comments
 (0)