Skip to content

Commit 91b6ab4

Browse files
authored
Bump to 2024 edition (#9)
1 parent 123086f commit 91b6ab4

File tree

10 files changed

+69
-73
lines changed

10 files changed

+69
-73
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ jobs:
66
runs-on: ubuntu-latest
77
steps:
88
- uses: actions/checkout@v3
9-
- uses: dtolnay/rust-toolchain@stable
9+
- uses: dtolnay/rust-toolchain@1.88.0
1010
- run: cargo build --all-features
1111
- run: cargo test --all-features

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ resolver = "2"
55
[workspace.package]
66
version = "0.4.0"
77
authors = ["Laurenz <[email protected]>"]
8-
edition = "2021"
8+
edition = "2024"
99
repository = "https://github.com/typst/comemo"
1010
readme = "README.md"
1111
license = "MIT OR Apache-2.0"

examples/calc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::collections::HashMap;
66
use std::path::{Path, PathBuf};
77

8-
use comemo::{memoize, track, Track, Tracked};
8+
use comemo::{Track, Tracked, memoize, track};
99

1010
fn main() {
1111
// Create some scripts in the calc language. This language supports addition

macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use proc_macro::TokenStream as BoundaryStream;
1818
use proc_macro2::TokenStream;
1919
use quote::{quote, quote_spanned};
2020
use syn::spanned::Spanned;
21-
use syn::{parse_quote, Error, Result};
21+
use syn::{Error, Result, parse_quote};
2222

2323
/// Memoize a function.
2424
///

macros/src/track.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@ fn prepare_method(vis: syn::Visibility, sig: &syn::Signature) -> Result<Method>
161161
kinds.push(kind)
162162
}
163163

164-
if let syn::ReturnType::Type(_, ty) = &sig.output {
165-
if let syn::Type::Reference(syn::TypeReference { mutability, .. }) = ty.as_ref() {
166-
if mutability.is_some() {
167-
bail!(ty, "tracked methods cannot return mutable references");
168-
}
169-
}
164+
if let syn::ReturnType::Type(_, ty) = &sig.output
165+
&& let syn::Type::Reference(syn::TypeReference { mutability, .. }) = ty.as_ref()
166+
&& mutability.is_some()
167+
{
168+
bail!(ty, "tracked methods cannot return mutable references");
170169
}
171170

172171
Ok(Method {
@@ -188,15 +187,15 @@ fn create_variants(methods: &[Method]) -> TokenStream {
188187
quote! { __ComemoVariant::#name(..) => #mutable }
189188
});
190189

191-
let is_mutable = (!methods.is_empty())
192-
.then(|| {
193-
quote! {
194-
match &self.0 {
195-
#(#is_mutable_variants),*
196-
}
190+
let is_mutable = if !methods.is_empty() {
191+
quote! {
192+
match &self.0 {
193+
#(#is_mutable_variants),*
197194
}
198-
})
199-
.unwrap_or_else(|| quote! { false });
195+
}
196+
} else {
197+
quote! { false}
198+
};
200199

201200
quote! {
202201
#[derive(Clone, PartialEq, Hash)]

src/constraint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
2-
use std::collections::hash_map::Entry;
32
use std::collections::HashMap;
3+
use std::collections::hash_map::Entry;
44
use std::hash::Hash;
55

66
use parking_lot::RwLock;

src/input.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ pub trait Input {
4545
impl<T: Hash> Input for T {
4646
// No constraint for hashed inputs.
4747
type Constraint = ();
48-
type Tracked<'r> = Self where Self: 'r;
48+
type Tracked<'r>
49+
= Self
50+
where
51+
Self: 'r;
4952
type Outer = ();
5053

5154
#[inline]
@@ -76,7 +79,10 @@ where
7679
{
7780
// Forward constraint from `Trackable` implementation.
7881
type Constraint = <T as Validate>::Constraint;
79-
type Tracked<'r> = Tracked<'r, T> where Self: 'r;
82+
type Tracked<'r>
83+
= Tracked<'r, T>
84+
where
85+
Self: 'r;
8086
type Outer = Option<&'a Self::Constraint>;
8187

8288
#[inline]
@@ -113,7 +119,10 @@ where
113119
{
114120
// Forward constraint from `Trackable` implementation.
115121
type Constraint = T::Constraint;
116-
type Tracked<'r> = TrackedMut<'r, T> where Self: 'r;
122+
type Tracked<'r>
123+
= TrackedMut<'r, T>
124+
where
125+
Self: 'r;
117126
type Outer = Option<&'a Self::Constraint>;
118127

119128
#[inline]

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ pub use comemo_macros::{memoize, track};
9999
pub mod internal {
100100
pub use parking_lot::RwLock;
101101

102-
pub use crate::cache::{memoized, register_evictor, Cache, CacheData};
103-
pub use crate::constraint::{hash, Call, ImmutableConstraint, MutableConstraint};
104-
pub use crate::input::{assert_hashable_or_trackable, Args, Input};
105-
pub use crate::track::{to_parts_mut_mut, to_parts_mut_ref, to_parts_ref, Surfaces};
102+
pub use crate::cache::{Cache, CacheData, memoized, register_evictor};
103+
pub use crate::constraint::{Call, ImmutableConstraint, MutableConstraint, hash};
104+
pub use crate::input::{Args, Input, assert_hashable_or_trackable};
105+
pub use crate::track::{Surfaces, to_parts_mut_mut, to_parts_mut_ref, to_parts_ref};
106106

107107
#[cfg(feature = "testing")]
108108
pub use crate::cache::last_was_hit;

src/track.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::constraint::Join;
1212
pub trait Track: Validate + Surfaces {
1313
/// Start tracking all accesses to a value.
1414
#[inline]
15-
fn track(&self) -> Tracked<Self> {
15+
fn track(&self) -> Tracked<'_, Self> {
1616
Tracked {
1717
value: self,
1818
constraint: None,
@@ -22,7 +22,7 @@ pub trait Track: Validate + Surfaces {
2222

2323
/// Start tracking all accesses and mutations to a value.
2424
#[inline]
25-
fn track_mut(&mut self) -> TrackedMut<Self> {
25+
fn track_mut(&mut self) -> TrackedMut<'_, Self> {
2626
TrackedMut { value: self, constraint: None }
2727
}
2828

@@ -293,7 +293,7 @@ where
293293

294294
/// Destructure a `Tracked<_>` into its parts.
295295
#[inline]
296-
pub fn to_parts_ref<T>(tracked: Tracked<T>) -> (&T, Option<&T::Constraint>)
296+
pub fn to_parts_ref<T>(tracked: Tracked<'_, T>) -> (&T, Option<&T::Constraint>)
297297
where
298298
T: Track + ?Sized,
299299
{

tests/tests.rs

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44
use std::hash::Hash;
55
use std::path::{Path, PathBuf};
66

7-
use comemo::{evict, memoize, track, Track, Tracked, TrackedMut, Validate};
7+
use comemo::{Track, Tracked, TrackedMut, Validate, evict, memoize, track};
88
use serial_test::serial;
99

1010
macro_rules! test {
@@ -39,11 +39,7 @@ fn test_basic() {
3939

4040
#[memoize]
4141
fn fib(n: u32) -> u32 {
42-
if n <= 2 {
43-
1
44-
} else {
45-
fib(n - 1) + fib(n - 2)
46-
}
42+
if n <= 2 { 1 } else { fib(n - 1) + fib(n - 2) }
4743
}
4844

4945
#[memoize]
@@ -213,11 +209,7 @@ fn test_kinds() {
213209

214210
#[memoize]
215211
fn unconditional(tester: Tracky) -> &'static str {
216-
if tester.by_value(Heavy("HEAVY".into())) > 10 {
217-
"Long"
218-
} else {
219-
"Short"
220-
}
212+
if tester.by_value(Heavy("HEAVY".into())) > 10 { "Long" } else { "Short" }
221213
}
222214

223215
let mut tester = Tester { data: "Hi".to_string() };
@@ -264,11 +256,7 @@ impl Tester {
264256

265257
/// Return value can borrow from both.
266258
fn double_ref<'a>(&'a self, name: &'a str) -> &'a str {
267-
if name.len() > self.data.len() {
268-
name
269-
} else {
270-
&self.data
271-
}
259+
if name.len() > self.data.len() { name } else { &self.data }
272260
}
273261

274262
/// Normal method with owned argument.
@@ -371,40 +359,40 @@ impl<'a> Chain<'a> {
371359
#[track]
372360
impl<'a> Chain<'a> {
373361
fn contains(&self, value: u32) -> bool {
374-
self.value == value || self.outer.map_or(false, |outer| outer.contains(value))
362+
self.value == value || self.outer.is_some_and(|outer| outer.contains(value))
375363
}
376364
}
377365

378366
/// Test mutable tracking.
379-
#[test]
380-
#[serial]
381-
#[rustfmt::skip]
382-
fn test_mutable() {
383-
#[comemo::memoize]
384-
fn dump(mut sink: TrackedMut<Emitter>) {
385-
sink.emit("a");
386-
sink.emit("b");
387-
let c = sink.len_or_ten().to_string();
388-
sink.emit(&c);
389-
}
390-
391-
let mut emitter = Emitter(vec![]);
392-
test!(miss: dump(emitter.track_mut()), ());
393-
test!(miss: dump(emitter.track_mut()), ());
394-
test!(miss: dump(emitter.track_mut()), ());
395-
test!(miss: dump(emitter.track_mut()), ());
396-
test!(hit: dump(emitter.track_mut()), ());
397-
test!(hit: dump(emitter.track_mut()), ());
398-
assert_eq!(emitter.0, [
399-
"a", "b", "2",
400-
"a", "b", "5",
401-
"a", "b", "8",
402-
"a", "b", "10",
403-
"a", "b", "10",
404-
"a", "b", "10",
405-
])
367+
#[test]
368+
#[serial]
369+
#[rustfmt::skip]
370+
fn test_mutable() {
371+
#[comemo::memoize]
372+
fn dump(mut sink: TrackedMut<Emitter>) {
373+
sink.emit("a");
374+
sink.emit("b");
375+
let c = sink.len_or_ten().to_string();
376+
sink.emit(&c);
406377
}
407378

379+
let mut emitter = Emitter(vec![]);
380+
test!(miss: dump(emitter.track_mut()), ());
381+
test!(miss: dump(emitter.track_mut()), ());
382+
test!(miss: dump(emitter.track_mut()), ());
383+
test!(miss: dump(emitter.track_mut()), ());
384+
test!(hit: dump(emitter.track_mut()), ());
385+
test!(hit: dump(emitter.track_mut()), ());
386+
assert_eq!(emitter.0, [
387+
"a", "b", "2",
388+
"a", "b", "5",
389+
"a", "b", "8",
390+
"a", "b", "10",
391+
"a", "b", "10",
392+
"a", "b", "10",
393+
])
394+
}
395+
408396
/// A tracked type with a mutable and an immutable method.
409397
#[derive(Clone)]
410398
struct Emitter(Vec<String>);

0 commit comments

Comments
 (0)