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 &T for Replacer where T: Replacer #83

Closed
RinCamelia opened this issue May 10, 2015 · 2 comments
Closed

implement &T for Replacer where T: Replacer #83

RinCamelia opened this issue May 10, 2015 · 2 comments
Labels
Milestone

Comments

@RinCamelia
Copy link

When attempting to call replace_all on a Regex with a str as the argument for rep(as opposed to a NoExpand), the Replacer trait implementation for str is apparently being ignored, instead falling back to the FnMut implementation. This project demonstrates the following compile error:

src\main.rs:13:23: 13:53 error: the trait `for<'r,'r> core::ops::Fn<(&'r regex::re::Captures<'r>,)>` is not implemented for the type `str` [E0277]
src\main.rs:13         true => regex.replace_all(&source, &replace.as_str()),

Where replace and source are both Strings.

Edit: just fixed the type of replace by adding as_str, as the & was not coercing the String to a str properly. See the latest commit.

@BurntSushi
Copy link
Member

If you use replace.as_str() instead of &replace.as_str(), then your code works. The as_str method returns a &str, so &replace.as_str() has type &&str, and no impl for that exists.

I wonder if we can add a impl<'a, T: Replacer> &'a T for Replacer { ... } impl that would fix this. The standard library uses this convention AFAIK.

@BurntSushi BurntSushi changed the title Replacer trait for str is being ignored to determine type info for replace argument on replace_all calls implement &T for Replacer where T: Replacer Jun 19, 2015
@BurntSushi BurntSushi added this to the 1.0 milestone Jan 31, 2016
@BurntSushi
Copy link
Member

I feel like it's probably not worth adding this impl unless there's a more solid reason. (It could be added in a backwards compatible manner later if we really wanted to, I think.)

BurntSushi pushed a commit that referenced this issue Mar 7, 2018
This permits use of a Replacer without consuming it.

Note: This can't simply return `&mut Self` because a generic
`impl<R: Replacer> Replacer for &mut R` would conflict with libstd's
generic `impl<F: FnMut> FnMut for &mut F`.

See also: #83

Closes #449
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants