Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Fn traits for Rc/Arc #34118

Closed
wants to merge 1 commit into from

Conversation

Stebalien
Copy link
Contributor

@Stebalien Stebalien commented Jun 6, 2016

This allows one to, e.g., use an Rc wrapped closure in Iterator::map. I would have implemented Fn and FnMut for Box as well but ran into coherence problems (see #33811).

@rust-highfive
Copy link
Collaborator

r? @aturon

(rust_highfive has picked a reviewer for you, use r? to override)

@Stebalien
Copy link
Contributor Author

Stebalien commented Jun 6, 2016

Make check pending. I guess you now do this for me using travis. My poor laptop thanks you.

/cc @bluss

@brson brson added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jun 6, 2016
@aturon
Copy link
Member

aturon commented Jun 7, 2016

Nice! cc @rust-lang/libs, I feel like we considered something like this before but ran into trouble; any concerns?

@alexcrichton
Copy link
Member

Note that in the strictest sense this is a breaking change because these traits are #[fundamental] (I think that's what that means, right?). In that sense we probably want to crater this before landing, but otherwise this seems fine to me!

@alexcrichton alexcrichton added the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Jun 7, 2016
@brson
Copy link
Contributor

brson commented Jun 10, 2016

I want to object to this feature, but can't think of any technical reasons.

What are the parallels to Box<Fn> that need to be considered? e.g. I don't see any equivalent Fn<I> for Box<F> impls.

@Stebalien
Copy link
Contributor Author

Stebalien commented Jun 10, 2016

@brson there are coherence issues with implementing this for Box<Fn> (due to the FnOnce stuff) that I couldn't work around (see #33811). I believe this should eventually be possible to implement (I hope).

My technical reason for wanting this is that it provides a way to create immutable (owned) closures that can be cloned (currently impossible, see #23501).

@bluss
Copy link
Member

bluss commented Jun 10, 2016

It makes sense, cloning closures is a good reason. I haven't needed it since you can clone &F where F: Fn instead, but this liberates it to more than stack scoped use cases.

@alexcrichton
Copy link
Member

The libs team was able to discuss this PR during triage today, but unfortunately we were unable to reach a conclusion. The stickler point was that these traits are #[fundamental] so it's a breaking change to add more impls of them to existing types. I believe @sfackler will update with more info as well.

@aturon
Copy link
Member

aturon commented Jun 21, 2016

@alexcrichton We should consider a crater run, just to gauge how much impact there actually is. While fundamental in principle means new impls are breaking, this is only the case if code is relying on the lack of those impls for coherence reasons, which is somewhat rare. It'd be worth talking through the overall semver policy here at some point.

@Stebalien
Copy link
Contributor Author

Also note, unless I'm mistaken, this can't break stable code (Fn etc. are unstable).

@sfackler
Copy link
Member

Won't this break anything that involves a blanket impl on Fn and an impl on Rc/Arc?

@aturon
Copy link
Member

aturon commented Jun 21, 2016

@Stebalien It affects stable code because downstream code gets the effect of fundamental even if it doesn't use it.

@sfackler Yes, that's roughly correct:

trait MyTrait {}

impl<T: FnOnce()> MyTrait for T {}
impl<T> MyTrait for Rc<T> {}

This code compiles today, and would break if we provided the proposed impls. What I'm trying to find out is how common this situation is. (I imagine in most cases the breakage would be handled by essentially deleting the Rc impl, but there may be more "interesting" cases.)

@Stebalien
Copy link
Contributor Author

@aturon, I see. I misunderstood fundamental.

@sfackler
Copy link
Member

@aturon but that solution would then break its use by older versions of rustc, right?

This seems pretty explicitly like something that we were very aware would be a breaking change when introducing #[fundamental]. It is why we haven't moved Error into core, and why I believe we're unwilling to stabilize #[fundamental] at all.

I honestly cringe a bit every time someone sees a breaking change and says "let's run crater". Crater can compile some fraction of the Rust code that is uploaded to crates.io, which is some fraction of Rust code that is publicly available, which is itself some fraction of Rust code that actually exists. It is incredibly useful to ease the transition for changes we have explicitly outlined as causing acceptable breakage (new trait impls etc), but if we keep relying to crater to find all of the breakage that exists when making arbitrary changes, we are going to get bit at some point. I'm worried our stability story is slowly drifting from what was outlined a year ago to "if a tree falls in the woods and I don't personally see anyone standing nearby, did it really make a sound?".

@alexcrichton
Copy link
Member

Looks like this causes breakage on crates.io, breaking both iron and rustful.

@Stebalien
Copy link
Contributor Author

😿

However, iron actually does something that this PR would have made impossible.

@Stebalien Stebalien closed this Jun 28, 2016
bors added a commit that referenced this pull request Apr 4, 2018
impl Fn/FnMut/FnOnce for Arc/Rc

This PR introduces the ability to have `Rc/Arc<F>: Fn(A) -> B where F: Fn(A) -> B` and `Rc/Arc<Fn(A) -> B`.

This was previously tried (and failed) in #34118 (comment) due to breakage with crater. But now that the new edition is approaching we might want to try this in edition 2018. We should probably do a new crater run to see how much the breakage has changed since the previous attempt.

cc @aturon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-crater Status: Waiting on a crater run to be completed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants