Skip to content

Commit 25923de

Browse files
nvzqzBurntSushi
authored andcommitted
api: add TryFrom impls for Regex
This enables builder APIs to take `T: TryInto<Regex>`, where `T` can be a string or a preconstructed `Regex` instance. Closes #1022
1 parent 55142aa commit 25923de

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Anchored search APIs are now available in `regex-automata 0.3`.
4040
Add new `Captures::extract` method for easier capture group access.
4141
* [FEATURE #961](https://github.com/rust-lang/regex/issues/961):
4242
Add `regex-lite` crate with smaller binary sizes and faster compile times.
43+
* [FEATURE #1022](https://github.com/rust-lang/regex/pull/1022):
44+
Add `TryFrom` implementations for the `Regex` type.
4345

4446
Performance improvements:
4547

regex-lite/src/string.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ impl core::str::FromStr for Regex {
117117
}
118118
}
119119

120+
impl TryFrom<&str> for Regex {
121+
type Error = Error;
122+
123+
/// Attempts to parse a string into a regular expression
124+
fn try_from(s: &str) -> Result<Regex, Error> {
125+
Regex::new(s)
126+
}
127+
}
128+
129+
impl TryFrom<String> for Regex {
130+
type Error = Error;
131+
132+
/// Attempts to parse a string into a regular expression
133+
fn try_from(s: String) -> Result<Regex, Error> {
134+
Regex::new(&s)
135+
}
136+
}
137+
120138
/// Core regular expression methods.
121139
impl Regex {
122140
/// Compiles a regular expression. Once compiled, it can be used repeatedly

src/regex/bytes.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::{borrow::Cow, sync::Arc, vec::Vec};
1+
use alloc::{borrow::Cow, string::String, sync::Arc, vec::Vec};
22

33
use regex_automata::{meta, util::captures, Input, PatternID};
44

@@ -124,6 +124,24 @@ impl core::str::FromStr for Regex {
124124
}
125125
}
126126

127+
impl TryFrom<&str> for Regex {
128+
type Error = Error;
129+
130+
/// Attempts to parse a string into a regular expression
131+
fn try_from(s: &str) -> Result<Regex, Error> {
132+
Regex::new(s)
133+
}
134+
}
135+
136+
impl TryFrom<String> for Regex {
137+
type Error = Error;
138+
139+
/// Attempts to parse a string into a regular expression
140+
fn try_from(s: String) -> Result<Regex, Error> {
141+
Regex::new(&s)
142+
}
143+
}
144+
127145
/// Core regular expression methods.
128146
impl Regex {
129147
/// Compiles a regular expression. Once compiled, it can be used repeatedly

src/regex/string.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ impl core::str::FromStr for Regex {
126126
}
127127
}
128128

129+
impl TryFrom<&str> for Regex {
130+
type Error = Error;
131+
132+
/// Attempts to parse a string into a regular expression
133+
fn try_from(s: &str) -> Result<Regex, Error> {
134+
Regex::new(s)
135+
}
136+
}
137+
138+
impl TryFrom<String> for Regex {
139+
type Error = Error;
140+
141+
/// Attempts to parse a string into a regular expression
142+
fn try_from(s: String) -> Result<Regex, Error> {
143+
Regex::new(&s)
144+
}
145+
}
146+
129147
/// Core regular expression methods.
130148
impl Regex {
131149
/// Compiles a regular expression. Once compiled, it can be used repeatedly

0 commit comments

Comments
 (0)