Skip to content

Commit f663d02

Browse files
authored
ci: enable colored CI logs and fix lifetime warnings (#353)
This PR enables colored output in CI logs and standardizes return types by explicitly specifying lifetimes for Cow and iterator methods. - Force Cargo and tests to emit color in CI by setting environment variables and test flags - Add explicit `'_` lifetimes to all `Cow<[u8]>` and iterator return types to fix warnings in nightly builds, see log example [before](https://github.com/dfinity/stable-structures/actions/runs/15823824228/job/44598984233#step:8:143) and [after](https://github.com/dfinity/stable-structures/actions/runs/15824248287/job/44600356815?pr=353#step:8:85)
1 parent 4404847 commit f663d02

File tree

13 files changed

+61
-57
lines changed

13 files changed

+61
-57
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
name: CI
22
on: [pull_request]
33

4+
env:
5+
CARGO_TERM_COLOR: always # Force Cargo to use colors
6+
TERM: xterm-256color
7+
48
jobs:
59
build:
610
runs-on: ubuntu-latest
@@ -37,7 +41,7 @@ jobs:
3741
run: cargo clippy --tests --benches -- -D clippy::all
3842

3943
- name: Test
40-
run: cargo test
44+
run: cargo test -- --color always
4145
env:
4246
RUST_BACKTRACE: 1
4347

benchmarks/src/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<const N: usize> Default for UnboundedVecN<N> {
2929
}
3030

3131
impl<const N: usize> Storable for UnboundedVecN<N> {
32-
fn to_bytes(&self) -> Cow<[u8]> {
32+
fn to_bytes(&self) -> Cow<'_, [u8]> {
3333
Cow::Owned(self.0.clone())
3434
}
3535

@@ -76,7 +76,7 @@ impl<const N: usize> Default for BoundedVecN<N> {
7676
}
7777

7878
impl<const N: usize> Storable for BoundedVecN<N> {
79-
fn to_bytes(&self) -> Cow<[u8]> {
79+
fn to_bytes(&self) -> Cow<'_, [u8]> {
8080
Cow::Owned(self.0.clone())
8181
}
8282

docs/src/schema-upgrades.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Asset {
1313
}
1414

1515
impl Storable for Asset {
16-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
16+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
1717
let mut bytes = vec![];
1818
ciborium::ser::into_writer(&self, &mut bytes).unwrap();
1919
Cow::Owned(bytes)

examples/src/custom_types_example/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct UserProfile {
2424
// The `Storable` trait is already implemented for several common types (e.g. u64),
2525
// so you can use those directly without implementing the `Storable` trait for them.
2626
impl Storable for UserProfile {
27-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
27+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
2828
Cow::Owned(Encode!(self).unwrap())
2929
}
3030

examples/src/vecs_and_strings/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const MAX_USER_DATA_SIZE: u32 = 100;
1717
struct UserName(String);
1818

1919
impl Storable for UserName {
20-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
20+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
2121
// String already implements `Storable`.
2222
self.0.to_bytes()
2323
}
@@ -39,7 +39,7 @@ impl Storable for UserName {
3939
struct UserData(Vec<u8>);
4040

4141
impl Storable for UserData {
42-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
42+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
4343
// Vec<u8> already implements `Storable`.
4444
self.0.to_bytes()
4545
}

fuzz/fuzz_targets/data.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ pub struct UnboundedFuzzStruct {
2020

2121
// The struct has size bounds reflected by Bound::Bounded::max_size
2222
impl Storable for BoundedFuzzStruct {
23-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
23+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
2424
Cow::Owned(serde_cbor::ser::to_vec(self).unwrap())
2525
}
2626

2727
fn into_bytes(self) -> Vec<u8> {
2828
serde_cbor::ser::to_vec(&self).unwrap()
2929
}
3030

31-
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
31+
fn from_bytes(bytes: std::borrow::Cow<'_, [u8]>) -> Self {
3232
let value: Self = serde_cbor::de::from_slice(bytes.as_ref()).unwrap();
3333
value
3434
}
@@ -41,15 +41,15 @@ impl Storable for BoundedFuzzStruct {
4141

4242
// The struct has no size bounds
4343
impl Storable for UnboundedFuzzStruct {
44-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
44+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
4545
Cow::Owned(serde_cbor::ser::to_vec(self).unwrap())
4646
}
4747

4848
fn into_bytes(self) -> Vec<u8> {
4949
serde_cbor::ser::to_vec(&self).unwrap()
5050
}
5151

52-
fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
52+
fn from_bytes(bytes: std::borrow::Cow<'_, [u8]>) -> Self {
5353
let value: Self = serde_cbor::de::from_slice(bytes.as_ref()).unwrap();
5454
value
5555
}

src/btreemap.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const PAGE_SIZE_VALUE_MARKER: u32 = u32::MAX;
172172
/// }
173173
///
174174
/// impl Storable for User {
175-
/// fn to_bytes(&self) -> Cow<[u8]> {
175+
/// fn to_bytes(&self) -> Cow<'_, [u8]> {
176176
/// let mut bytes = Vec::new();
177177
/// // TODO: Convert your struct to bytes...
178178
/// Cow::Owned(bytes)
@@ -1161,7 +1161,7 @@ where
11611161
/// println!("{}: {}", key, value);
11621162
/// }
11631163
/// ```
1164-
pub fn iter(&self) -> Iter<K, V, M> {
1164+
pub fn iter(&self) -> Iter<'_, K, V, M> {
11651165
self.iter_internal().into()
11661166
}
11671167

@@ -1184,7 +1184,7 @@ where
11841184
/// println!("{}: {}", key, value);
11851185
/// }
11861186
/// ```
1187-
pub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<K, V, M> {
1187+
pub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<'_, K, V, M> {
11881188
self.range_internal(key_range).into()
11891189
}
11901190

@@ -1194,7 +1194,7 @@ where
11941194
/// Useful when `range(bound..)` skips the previous element.
11951195
///
11961196
/// Returns an empty iterator if no smaller key exists.
1197-
pub fn iter_from_prev_key(&self, bound: &K) -> Iter<K, V, M> {
1197+
pub fn iter_from_prev_key(&self, bound: &K) -> Iter<'_, K, V, M> {
11981198
if let Some((start_key, _)) = self.range(..bound).next_back() {
11991199
IterInternal::new_in_range(self, (Bound::Included(start_key), Bound::Unbounded)).into()
12001200
} else {
@@ -1211,36 +1211,36 @@ where
12111211
/// The new name, [`iter_from_prev_key`], better reflects this behavior and
12121212
/// improves code clarity.
12131213
#[deprecated(note = "use `iter_from_prev_key` instead")]
1214-
pub fn iter_upper_bound(&self, bound: &K) -> Iter<K, V, M> {
1214+
pub fn iter_upper_bound(&self, bound: &K) -> Iter<'_, K, V, M> {
12151215
self.iter_from_prev_key(bound)
12161216
}
12171217

12181218
/// Returns an iterator over the keys of the map.
1219-
pub fn keys(&self) -> KeysIter<K, V, M> {
1219+
pub fn keys(&self) -> KeysIter<'_, K, V, M> {
12201220
self.iter_internal().into()
12211221
}
12221222

12231223
/// Returns an iterator over the keys of the map which belong to the specified range.
1224-
pub fn keys_range(&self, key_range: impl RangeBounds<K>) -> KeysIter<K, V, M> {
1224+
pub fn keys_range(&self, key_range: impl RangeBounds<K>) -> KeysIter<'_, K, V, M> {
12251225
self.range_internal(key_range).into()
12261226
}
12271227

12281228
/// Returns an iterator over the values of the map, sorted by key.
1229-
pub fn values(&self) -> ValuesIter<K, V, M> {
1229+
pub fn values(&self) -> ValuesIter<'_, K, V, M> {
12301230
self.iter_internal().into()
12311231
}
12321232

12331233
/// Returns an iterator over the values of the map where keys
12341234
/// belong to the specified range.
1235-
pub fn values_range(&self, key_range: impl RangeBounds<K>) -> ValuesIter<K, V, M> {
1235+
pub fn values_range(&self, key_range: impl RangeBounds<K>) -> ValuesIter<'_, K, V, M> {
12361236
self.range_internal(key_range).into()
12371237
}
12381238

1239-
fn iter_internal(&self) -> IterInternal<K, V, M> {
1239+
fn iter_internal(&self) -> IterInternal<'_, K, V, M> {
12401240
IterInternal::new(self)
12411241
}
12421242

1243-
fn range_internal(&self, key_range: impl RangeBounds<K>) -> IterInternal<K, V, M> {
1243+
fn range_internal(&self, key_range: impl RangeBounds<K>) -> IterInternal<'_, K, V, M> {
12441244
if self.root_addr == NULL {
12451245
// Map is empty.
12461246
return IterInternal::null(self);
@@ -2999,7 +2999,7 @@ mod test {
29992999
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq)]
30003000
struct BuggyStruct;
30013001
impl crate::Storable for BuggyStruct {
3002-
fn to_bytes(&self) -> Cow<[u8]> {
3002+
fn to_bytes(&self) -> Cow<'_, [u8]> {
30033003
Cow::Borrowed(&[1, 2, 3, 4])
30043004
}
30053005

@@ -3142,7 +3142,7 @@ mod test {
31423142
#[derive(PartialOrd, Ord, Clone, Eq, PartialEq, Debug)]
31433143
struct T;
31443144
impl Storable for T {
3145-
fn to_bytes(&self) -> Cow<[u8]> {
3145+
fn to_bytes(&self) -> Cow<'_, [u8]> {
31463146
Cow::Borrowed(&[1, 2, 3])
31473147
}
31483148

@@ -3165,7 +3165,7 @@ mod test {
31653165
#[derive(PartialOrd, Ord, Clone, Eq, PartialEq, Debug)]
31663166
struct T2;
31673167
impl Storable for T2 {
3168-
fn to_bytes(&self) -> Cow<[u8]> {
3168+
fn to_bytes(&self) -> Cow<'_, [u8]> {
31693169
Cow::Owned(vec![1, 2, 3])
31703170
}
31713171

src/btreemap/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<K: Storable + Ord + Clone> Node<K> {
178178

179179
/// Returns a reference to the entry at the specified index.
180180
#[inline(always)]
181-
pub fn entry<M: Memory>(&self, idx: usize, memory: &M) -> EntryRef<K> {
181+
pub fn entry<M: Memory>(&self, idx: usize, memory: &M) -> EntryRef<'_, K> {
182182
(self.key(idx, memory), self.value(idx, memory))
183183
}
184184

src/btreeset.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ where
108108
/// }
109109
///
110110
/// impl Storable for CustomType {
111-
/// fn to_bytes(&self) -> Cow<[u8]> {
111+
/// fn to_bytes(&self) -> Cow<'_, [u8]> {
112112
/// Cow::Owned(self.id.to_le_bytes().to_vec())
113113
/// }
114114
///
@@ -482,7 +482,7 @@ where
482482
/// println!("{}", key);
483483
/// }
484484
/// ```
485-
pub fn iter(&self) -> Iter<K, M> {
485+
pub fn iter(&self) -> Iter<'_, K, M> {
486486
Iter::new(self.map.iter())
487487
}
488488

@@ -506,7 +506,7 @@ where
506506
/// let range: Vec<_> = set.range(2..).collect();
507507
/// assert_eq!(range, vec![2, 3]);
508508
/// ```
509-
pub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<K, M> {
509+
pub fn range(&self, key_range: impl RangeBounds<K>) -> Iter<'_, K, M> {
510510
Iter::new(self.map.range(key_range))
511511
}
512512

0 commit comments

Comments
 (0)