Skip to content

Commit 20b1c20

Browse files
authored
Simplify lifetime setup (#13)
1 parent 2ce0d0a commit 20b1c20

File tree

3 files changed

+32
-71
lines changed

3 files changed

+32
-71
lines changed

macros/src/memoize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn process(function: &Function) -> Result<TokenStream> {
134134
// Construct the inner closure.
135135
let output = &function.output;
136136
let body = &function.item.block;
137-
let closure = quote! { |#param_tuple| -> #output #body };
137+
let closure = quote! { |::comemo::internal::Args(#param_tuple)| -> #output #body };
138138

139139
// Adjust the function's body.
140140
let mut wrapped = function.item.clone();

src/cache.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ thread_local! {
1919
}
2020

2121
/// Execute a function or use a cached result for it.
22-
pub fn memoized<'c, In, Out, F>(
22+
pub fn memoized<'a, In, Out, F>(
2323
mut input: In,
24-
constraint: &'c In::Constraint,
24+
constraint: &'a In::Constraint,
2525
cache: &Cache<In::Constraint, Out>,
2626
enabled: bool,
2727
func: F,
2828
) -> Out
2929
where
30-
In: Input + 'c,
30+
In: Input<'a>,
3131
Out: Clone + 'static,
32-
F: FnOnce(In::Tracked<'c>) -> Out,
32+
F: FnOnce(In) -> Out,
3333
{
3434
// Early bypass if memoization is disabled.
3535
// Hopefully the compiler will optimize this away, if the condition is constant.
@@ -80,15 +80,15 @@ where
8080
output
8181
}
8282

83-
fn memoized_disabled<'c, In, Out, F>(
83+
fn memoized_disabled<'a, In, Out, F>(
8484
input: In,
85-
constraint: &'c In::Constraint,
85+
constraint: &'a In::Constraint,
8686
func: F,
8787
) -> Out
8888
where
89-
In: Input + 'c,
89+
In: Input<'a>,
9090
Out: Clone + 'static,
91-
F: FnOnce(In::Tracked<'c>) -> Out,
91+
F: FnOnce(In) -> Out,
9292
{
9393
// Execute the function with the new constraints hooked in.
9494
let (input, outer) = input.retrack(constraint);
@@ -168,9 +168,9 @@ impl<C, Out: 'static> CacheData<C, Out> {
168168
}
169169

170170
/// Look for a matching entry in the cache.
171-
fn lookup<In>(&self, key: u128, input: &In) -> Option<(&In::Constraint, &Out)>
171+
fn lookup<'a, In>(&self, key: u128, input: &In) -> Option<(&In::Constraint, &Out)>
172172
where
173-
In: Input<Constraint = C>,
173+
In: Input<'a, Constraint = C>,
174174
{
175175
self.entries
176176
.get(&key)?
@@ -180,9 +180,9 @@ impl<C, Out: 'static> CacheData<C, Out> {
180180
}
181181

182182
/// Insert an entry into the cache.
183-
fn insert<In>(&mut self, key: u128, constraint: In::Constraint, output: Out)
183+
fn insert<'a, In>(&mut self, key: u128, constraint: In::Constraint, output: Out)
184184
where
185-
In: Input<Constraint = C>,
185+
In: Input<'a, Constraint = C>,
186186
{
187187
self.entries
188188
.entry(key)
@@ -209,17 +209,17 @@ struct CacheEntry<C, Out> {
209209

210210
impl<C, Out: 'static> CacheEntry<C, Out> {
211211
/// Create a new entry.
212-
fn new<In>(constraint: In::Constraint, output: Out) -> Self
212+
fn new<'a, In>(constraint: In::Constraint, output: Out) -> Self
213213
where
214-
In: Input<Constraint = C>,
214+
In: Input<'a, Constraint = C>,
215215
{
216216
Self { constraint, output, age: AtomicUsize::new(0) }
217217
}
218218

219219
/// Return the entry's output if it is valid for the given input.
220-
fn lookup<In>(&self, input: &In) -> Option<(&In::Constraint, &Out)>
220+
fn lookup<'a, In>(&self, input: &In) -> Option<(&In::Constraint, &Out)>
221221
where
222-
In: Input<Constraint = C>,
222+
In: Input<'a, Constraint = C>,
223223
{
224224
input.validate(&self.constraint).then(|| {
225225
self.age.store(0, Ordering::SeqCst);

src/input.rs

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,16 @@ use crate::track::{Track, Tracked, TrackedMut, Validate};
55

66
/// Ensure a type is suitable as input.
77
#[inline]
8-
pub fn assert_hashable_or_trackable<In: Input>(_: &In) {}
8+
pub fn assert_hashable_or_trackable<'a, In: Input<'a>>(_: &In) {}
99

1010
/// An input to a cached function.
1111
///
1212
/// This is implemented for hashable types, `Tracked<_>` types and `Args<(...)>`
1313
/// types containing tuples up to length twelve.
14-
pub trait Input {
14+
pub trait Input<'a> {
1515
/// The constraints for this input.
1616
type Constraint: Default + Clone + Join + 'static;
1717

18-
/// The input with new constraints hooked in.
19-
type Tracked<'r>
20-
where
21-
Self: 'r;
22-
2318
/// The extracted outer constraints.
2419
type Outer: Join<Self::Constraint>;
2520

@@ -34,21 +29,14 @@ pub trait Input {
3429

3530
/// Hook up the given constraint to the tracked parts of the input and
3631
/// return the result alongside the outer constraints.
37-
fn retrack<'r>(
38-
self,
39-
constraint: &'r Self::Constraint,
40-
) -> (Self::Tracked<'r>, Self::Outer)
32+
fn retrack(self, constraint: &'a Self::Constraint) -> (Self, Self::Outer)
4133
where
42-
Self: 'r;
34+
Self: Sized;
4335
}
4436

45-
impl<T: Hash> Input for T {
37+
impl<'a, T: Hash> Input<'a> for T {
4638
// No constraint for hashed inputs.
4739
type Constraint = ();
48-
type Tracked<'r>
49-
= Self
50-
where
51-
Self: 'r;
5240
type Outer = ();
5341

5442
#[inline]
@@ -65,24 +53,17 @@ impl<T: Hash> Input for T {
6553
fn replay(&mut self, _: &Self::Constraint) {}
6654

6755
#[inline]
68-
fn retrack<'r>(self, _: &'r ()) -> (Self::Tracked<'r>, Self::Outer)
69-
where
70-
Self: 'r,
71-
{
56+
fn retrack(self, _: &'a ()) -> (Self, Self::Outer) {
7257
(self, ())
7358
}
7459
}
7560

76-
impl<'a, T> Input for Tracked<'a, T>
61+
impl<'a, T> Input<'a> for Tracked<'a, T>
7762
where
7863
T: Track + ?Sized,
7964
{
8065
// Forward constraint from `Trackable` implementation.
8166
type Constraint = <T as Validate>::Constraint;
82-
type Tracked<'r>
83-
= Tracked<'r, T>
84-
where
85-
Self: 'r;
8667
type Outer = Option<&'a Self::Constraint>;
8768

8869
#[inline]
@@ -97,13 +78,7 @@ where
9778
fn replay(&mut self, _: &Self::Constraint) {}
9879

9980
#[inline]
100-
fn retrack<'r>(
101-
self,
102-
constraint: &'r Self::Constraint,
103-
) -> (Self::Tracked<'r>, Self::Outer)
104-
where
105-
Self: 'r,
106-
{
81+
fn retrack(self, constraint: &'a Self::Constraint) -> (Self, Self::Outer) {
10782
let tracked = Tracked {
10883
value: self.value,
10984
constraint: Some(constraint),
@@ -113,16 +88,12 @@ where
11388
}
11489
}
11590

116-
impl<'a, T> Input for TrackedMut<'a, T>
91+
impl<'a, T> Input<'a> for TrackedMut<'a, T>
11792
where
11893
T: Track + ?Sized,
11994
{
12095
// Forward constraint from `Trackable` implementation.
12196
type Constraint = T::Constraint;
122-
type Tracked<'r>
123-
= TrackedMut<'r, T>
124-
where
125-
Self: 'r;
12697
type Outer = Option<&'a Self::Constraint>;
12798

12899
#[inline]
@@ -139,13 +110,7 @@ where
139110
}
140111

141112
#[inline]
142-
fn retrack<'r>(
143-
self,
144-
constraint: &'r Self::Constraint,
145-
) -> (Self::Tracked<'r>, Self::Outer)
146-
where
147-
Self: 'r,
148-
{
113+
fn retrack(self, constraint: &'a Self::Constraint) -> (Self, Self::Outer) {
149114
let tracked = TrackedMut { value: self.value, constraint: Some(constraint) };
150115
(tracked, self.constraint)
151116
}
@@ -157,9 +122,8 @@ pub struct Args<T>(pub T);
157122
macro_rules! args_input {
158123
($($param:tt $alt:tt $idx:tt ),*) => {
159124
#[allow(unused_variables, non_snake_case)]
160-
impl<$($param: Input),*> Input for Args<($($param,)*)> {
125+
impl<'a, $($param: Input<'a>),*> Input<'a> for Args<($($param,)*)> {
161126
type Constraint = ($($param::Constraint,)*);
162-
type Tracked<'r> = ($($param::Tracked<'r>,)*) where Self: 'r;
163127
type Outer = ($($param::Outer,)*);
164128

165129
#[inline]
@@ -178,15 +142,12 @@ macro_rules! args_input {
178142
}
179143

180144
#[inline]
181-
fn retrack<'r>(
145+
fn retrack(
182146
self,
183-
constraint: &'r Self::Constraint,
184-
) -> (Self::Tracked<'r>, Self::Outer)
185-
where
186-
Self: 'r,
187-
{
147+
constraint: &'a Self::Constraint,
148+
) -> (Self, Self::Outer) {
188149
$(let $param = (self.0).$idx.retrack(&constraint.$idx);)*
189-
(($($param.0,)*), ($($param.1,)*))
150+
(Args(($($param.0,)*)), ($($param.1,)*))
190151
}
191152
}
192153

0 commit comments

Comments
 (0)