-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
/cc @bluss |
ee8145b
to
79aec55
Compare
Nice! cc @rust-lang/libs, I feel like we considered something like this before but ran into trouble; any concerns? |
Note that in the strictest sense this is a breaking change because these traits are |
I want to object to this feature, but can't think of any technical reasons. What are the parallels to |
@brson there are coherence issues with implementing this for 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). |
It makes sense, cloning closures is a good reason. I haven't needed it since you can clone |
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 |
@alexcrichton We should consider a crater run, just to gauge how much impact there actually is. While |
Also note, unless I'm mistaken, this can't break stable code ( |
Won't this break anything that involves a blanket impl on Fn and an impl on Rc/Arc? |
@Stebalien It affects stable code because downstream code gets the effect of @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 |
@aturon, I see. I misunderstood fundamental. |
@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 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?". |
Looks like this causes breakage on crates.io, breaking both iron and rustful. |
😿 However, iron actually does something that this PR would have made impossible. |
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
This allows one to, e.g., use an
Rc
wrapped closure inIterator::map
. I would have implementedFn
andFnMut
forBox
as well but ran into coherence problems (see #33811).