regex icon indicating copy to clipboard operation
regex copied to clipboard

feature request: Add `impl TryFrom<Arc<str>> for Regex`

Open al8n opened this issue 9 months ago • 7 comments

Hi, thanks for the amazing crate!

As the internal pattern is an Arc<str>, is it possible to add impl From<Regex> for Arc<str> and impl From<&Regex> for Arc<str> implementation? It is useful when doing conversions between types.

al8n avatar Apr 04 '25 15:04 al8n

Well, it certainly can't be From<Arc<str>>. I would be open to a impl TryFrom<Arc<str>> for Regex though, since that's consistent with existing TryFrom<&str> and TryFrom<String> impls.

I don't see why there should be an impl From<&Regex> for Arc<str> though. There is already a Regex::as_str, and implementing From<&Regex> for the various string types seems a little weird to me. I'm unclear on the use case.

As the internal pattern is an Arc<str>

The internal representation should have absolutely nothing to do with this. If you're mentioning this because you think it makes the conversion cheaper, then that is almost certainly not true in any meaningful sense.

BurntSushi avatar Apr 04 '25 16:04 BurntSushi

Well, it certainly can't be From<Arc<str>>. I would be open to a impl TryFrom<Arc<str>> for Regex though, since that's consistent with existing TryFrom<&str> and TryFrom<String> impls.

I don't see why there should be an impl From<&Regex> for Arc<str> though. There is already a Regex::as_str, and implementing From<&Regex> for the various string types seems a little weird to me. I'm unclear on the use case.

As the internal pattern is an Arc<str>

The internal representation should have absolutely nothing to do with this. If you're mentioning this because you think it makes the conversion cheaper, then that is almost certainly not true in any meaningful sense.

The use case for impl From<Regex> for Arc<str> and impl From<&Regex> for Arc<str> is FFI, indeed, the current API, users can get the &str, but it will require another allocation to construct a new Arc<str> for FFI

al8n avatar Apr 04 '25 19:04 al8n

That isn't a compelling enough motivation on its own IMO. That impl would require regex to always internally have an Arc<str>, and this may not always be true. Indeed, it hasn't always been true.

BurntSushi avatar Apr 04 '25 19:04 BurntSushi

That isn't a compelling enough motivation on its own IMO. That impl would require regex to always internally have an Arc<str>, and this may not always be true. Indeed, it hasn't always been true.

Got it, yes, the internal may change in the future.

al8n avatar Apr 05 '25 06:04 al8n

I don't get why you closed this? I did say that adding impl TryFrom<Arc<str>> for Regex should be totally fine.

BurntSushi avatar Apr 05 '25 11:04 BurntSushi

I don't get why you closed this? I did say that adding impl TryFrom<Arc<str>> for Regex should be totally fine.

I closed this because impl From<Regex> for Arc<str> will not be added. May be I should change the title of this issue to impl TryFrom<Arc<str>> for Regex?

al8n avatar Apr 05 '25 12:04 al8n

Ah I see. I think a misread that your request wanted a From<Arc<str>> for Regex impl. My bad. But I think that is a valid thing to want, so I changed the title to reflect that. But I get why you closed this now. Sorry for the misunderstanding!

BurntSushi avatar Apr 05 '25 12:04 BurntSushi