Skip to content

Commit 1d3a5ca

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

File tree

5 files changed

+149
-105
lines changed

5 files changed

+149
-105
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: 8 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,8 @@ 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()
316+
.copied()
319317
.enumerate()
320318
.map(|(i, v)| {
321319
let idx = source_end + i;
@@ -367,8 +365,8 @@ fn hirschberg_impl<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
367365
.unwrap();
368366

369367
let left = hirschberg_impl(
370-
&target,
371-
&source,
368+
target,
369+
source,
372370
Indices {
373371
target_end: target_split_index,
374372
source_end: source_split_index,
@@ -377,8 +375,8 @@ fn hirschberg_impl<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
377375
);
378376

379377
let right = hirschberg_impl(
380-
&target,
381-
&source,
378+
target,
379+
source,
382380
Indices {
383381
target_start: target_split_index,
384382
source_start: source_split_index,
@@ -561,8 +559,8 @@ fn levenshtein_impl<'src, 'target: 'src, T: Clone + PartialEq + 'target>(
561559

562560
changelist_from_change_table(
563561
table,
564-
&target,
565-
&source,
562+
target,
563+
source,
566564
Indices {
567565
target_start,
568566
target_end,

src/collections/rope.rs

Lines changed: 26 additions & 17 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,13 @@ 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
175+
.self_ref
176+
.0
177+
.get(key)
178+
.and_then(|v| v.get(new_in_key))
179+
.is_some()
180+
{
177181
self.key = Into::<usize>::into(key);
178182
self.in_key = new_in_key;
179183
return ret;
@@ -238,11 +242,11 @@ impl<'rope, T: 'rope> IntoIterator for &'rope Rope<T> {
238242

239243
impl<T> FromIterator<T> for Rope<T> {
240244
fn from_iter<C: IntoIterator<Item = T>>(iter: C) -> Self {
241-
let mut iter = iter.into_iter();
245+
let iter = iter.into_iter();
242246
let mut counter = 0;
243247
let mut current = VecDeque::with_capacity(MAX_SLOT_SIZE);
244248
let mut map = BTreeMap::new();
245-
while let Some(item) = iter.next() {
249+
for item in iter {
246250
current.push_back(item);
247251
counter += 1;
248252
if counter % DEF_SLOT_SIZE == 0 {
@@ -261,6 +265,12 @@ impl<T> FromIterator<T> for Rope<T> {
261265
}
262266
}
263267

268+
impl<T> Default for Rope<T> {
269+
fn default() -> Self {
270+
Self::new()
271+
}
272+
}
273+
264274
impl<T> Rope<T> {
265275
pub fn new() -> Self {
266276
Self(BTreeMap::from([(
@@ -309,8 +319,7 @@ impl<T> Rope<T> {
309319
let prev_high_index = self
310320
.0
311321
.range(..key)
312-
.rev()
313-
.next()
322+
.next_back()
314323
.map(|(k, _)| k.clone())
315324
.unwrap_or_default();
316325
let keys: Vec<Key> = self
@@ -323,7 +332,7 @@ impl<T> Rope<T> {
323332
let mut hold = VecDeque::<T>::with_capacity(0);
324333

325334
for key in keys.iter() {
326-
let entry = self.0.get_mut(&key).unwrap();
335+
let entry = self.0.get_mut(key).unwrap();
327336
if entry.is_empty() {
328337
continue;
329338
}
@@ -358,7 +367,7 @@ impl<T> Rope<T> {
358367
}
359368

360369
// 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);
370+
std::mem::swap(self.0.get_mut(key).unwrap(), &mut hold);
362371
}
363372

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

src/lib.rs

Lines changed: 109 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,123 @@ 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+
pub trait StructDiffOwnedBound: Clone {}
112+
impl<T: Clone> StructDiffOwnedBound for T {}
113+
114+
pub trait StructDiffRefBound: Clone {}
115+
impl<T: Clone> StructDiffRefBound for T {}
116+
}
117+
11118
pub trait StructDiff {
12119
/// A generated type used to represent the difference
13120
/// between two instances of a struct which implements
14121
/// 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;
122+
type Diff: __private::StructDiffOwnedBound;
47123

48124
/// A generated type used to represent the difference
49125
/// between two instances of a struct which implements
50126
/// 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>
127+
type DiffRef<'target>: __private::StructDiffRefBound + Into<Self::Diff>
97128
where
98129
Self: 'target;
99130

0 commit comments

Comments
 (0)