Skip to content

Make new_with_timer accept a closure - #5

Closed
tomaka wants to merge 2 commits into
rust-random:masterfrom
tomaka:new-with-timer-less-cumbersome
Closed

tomaka wants to merge 2 commits into
rust-random:masterfrom
tomaka:new-with-timer-less-cumbersome

Conversation

@tomaka

@tomaka tomaka commented Mar 15, 2020

Copy link
Copy Markdown
Contributor

Turns JitterRng::new_with_timer(fn() -> u64) into new_with_timer(impl Fn() -> u64).
This allows passing a closure to this function, and to properly manage ownership.

I don't generally know what the opinion about this change would be, but I thought I'd just open a PR and see.

@dhardy

dhardy commented Mar 15, 2020

Copy link
Copy Markdown
Member

Huh, I haven't seen any interest in JitterRng in a while!

I see you store Arc<dyn Fn() -> u64 + Send + Sync> — essentially a reference-counted function pointer; but since you also require 'static I think this is no more flexible than &dyn Fn() -> u64 + Send + Sync?

I'm not a big fan of APIT. Perhaps instead use F: Into<&dyn Fn() -> u64 + Send + Sync>?

@tomaka

tomaka commented Mar 15, 2020

Copy link
Copy Markdown
Contributor Author

but since you also require 'static I think this is no more flexible than &dyn Fn() -> u64 + Send + Sync?

I'm not following. Are you suggesting doing &'static dyn Fn() -> u64 + Send + Sync, or are you suggesting &'a dyn Fn() -> u64 + Send + Sync + 'a with 'a being a lifetime on the JitterRng?

@dhardy

dhardy commented Mar 15, 2020

Copy link
Copy Markdown
Member

What happens with the current code in your PR? I guess it requires &'static anyway?

@tomaka

tomaka commented Mar 15, 2020

Copy link
Copy Markdown
Contributor Author

With my PR, the closure itself can only hold things by value and not by reference, but the closure will be deleted when the JitterRng gets dropped. Whereas with your suggestion the closure will never be destroyed and you essentially have a small memory leak.

@dhardy

dhardy commented Mar 15, 2020

Copy link
Copy Markdown
Member

Your PR accepts timer: impl Fn() -> u64 + Send + Sync + 'static which means the closure cannot have any allocations, mutable data, or references which are not 'static (i.e. the closure has no functionality over a function pointer). So the two are equivalent, except that yours has the overhead of an Arc.

@tomaka

tomaka commented Mar 16, 2020

Copy link
Copy Markdown
Contributor Author

I don't get it.

With my PR, you can do something like:

let at_start = now();
let rng = JitterRng::new_with_timer(move || { now() - at_start });

You cannot do that right now and you cannot do if you have to pass a &'static dyn Fn() -> u64 + Send + Sync.

@tomaka

tomaka commented Mar 16, 2020

Copy link
Copy Markdown
Contributor Author

@dhardy

dhardy commented Mar 16, 2020

Copy link
Copy Markdown
Member

You're right, a closure can contain read-only data (including allocations since Box<T>: 'static).

@dhardy

dhardy commented Mar 16, 2020

Copy link
Copy Markdown
Member

Your PR introduces a dependency on alloc, and it's not an optional one. Until now, this crate only required core. So I'm not sure what to do about this.

One option might be to feature-gate alloc and only implement Clone when using alloc — IIUC this is the only time we need Arc. Of course this is still a breaking change.

What is your use-case, if you don't mind me asking?

@vks

vks commented Sep 4, 2020

Copy link
Copy Markdown
Contributor

What about just dropping the Clone implementation?

@vks vks closed this in #9 Sep 7, 2020
@dhardy dhardy mentioned this pull request Dec 16, 2020
@tomaka
tomaka deleted the new-with-timer-less-cumbersome branch December 27, 2020 10:49
@tomaka
tomaka restored the new-with-timer-less-cumbersome branch December 27, 2020 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants