Skip to content

Commit

Permalink
api: add TryFrom impls for Regex
Browse files Browse the repository at this point in the history
This enables builder APIs to take `T: TryInto<Regex>`, where `T` can be
a string or a preconstructed `Regex` instance.

Closes #1022
  • Loading branch information
nvzqz authored and BurntSushi committed Jul 5, 2023
1 parent 55142aa commit 25923de
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Anchored search APIs are now available in `regex-automata 0.3`.
Add new `Captures::extract` method for easier capture group access.
* [FEATURE #961](https://github.com/rust-lang/regex/issues/961):
Add `regex-lite` crate with smaller binary sizes and faster compile times.
* [FEATURE #1022](https://github.com/rust-lang/regex/pull/1022):
Add `TryFrom` implementations for the `Regex` type.

Performance improvements:

Expand Down
18 changes: 18 additions & 0 deletions regex-lite/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ impl core::str::FromStr for Regex {
}
}

impl TryFrom<&str> for Regex {
type Error = Error;

/// Attempts to parse a string into a regular expression
fn try_from(s: &str) -> Result<Regex, Error> {
Regex::new(s)
}
}

impl TryFrom<String> for Regex {
type Error = Error;

/// Attempts to parse a string into a regular expression
fn try_from(s: String) -> Result<Regex, Error> {
Regex::new(&s)
}
}

/// Core regular expression methods.
impl Regex {
/// Compiles a regular expression. Once compiled, it can be used repeatedly
Expand Down
20 changes: 19 additions & 1 deletion src/regex/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{borrow::Cow, sync::Arc, vec::Vec};
use alloc::{borrow::Cow, string::String, sync::Arc, vec::Vec};

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

Expand Down Expand Up @@ -124,6 +124,24 @@ impl core::str::FromStr for Regex {
}
}

impl TryFrom<&str> for Regex {
type Error = Error;

/// Attempts to parse a string into a regular expression
fn try_from(s: &str) -> Result<Regex, Error> {
Regex::new(s)
}
}

impl TryFrom<String> for Regex {
type Error = Error;

/// Attempts to parse a string into a regular expression
fn try_from(s: String) -> Result<Regex, Error> {
Regex::new(&s)
}
}

/// Core regular expression methods.
impl Regex {
/// Compiles a regular expression. Once compiled, it can be used repeatedly
Expand Down
18 changes: 18 additions & 0 deletions src/regex/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ impl core::str::FromStr for Regex {
}
}

impl TryFrom<&str> for Regex {
type Error = Error;

/// Attempts to parse a string into a regular expression
fn try_from(s: &str) -> Result<Regex, Error> {
Regex::new(s)
}
}

impl TryFrom<String> for Regex {
type Error = Error;

/// Attempts to parse a string into a regular expression
fn try_from(s: String) -> Result<Regex, Error> {
Regex::new(&s)
}
}

/// Core regular expression methods.
impl Regex {
/// Compiles a regular expression. Once compiled, it can be used repeatedly
Expand Down

0 comments on commit 25923de

Please sign in to comment.