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 TryFrom<{&str, String}> for Regex #1022

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions regex-lite/src/string.rs
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
@@ -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
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