diff --git a/regex-lite/src/string.rs b/regex-lite/src/string.rs index 358f7a0c7..acf6aa00a 100644 --- a/regex-lite/src/string.rs +++ b/regex-lite/src/string.rs @@ -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::new(s) + } +} + +impl TryFrom for Regex { + type Error = Error; + + /// Attempts to parse a string into a regular expression + fn try_from(s: String) -> Result { + Regex::new(&s) + } +} + /// Core regular expression methods. impl Regex { /// Compiles a regular expression. Once compiled, it can be used repeatedly diff --git a/src/regex/bytes.rs b/src/regex/bytes.rs index fc4238bcd..86437e1c2 100644 --- a/src/regex/bytes.rs +++ b/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}; @@ -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::new(s) + } +} + +impl TryFrom for Regex { + type Error = Error; + + /// Attempts to parse a string into a regular expression + fn try_from(s: String) -> Result { + Regex::new(&s) + } +} + /// Core regular expression methods. impl Regex { /// Compiles a regular expression. Once compiled, it can be used repeatedly diff --git a/src/regex/string.rs b/src/regex/string.rs index 438af7beb..e8d625655 100644 --- a/src/regex/string.rs +++ b/src/regex/string.rs @@ -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::new(s) + } +} + +impl TryFrom for Regex { + type Error = Error; + + /// Attempts to parse a string into a regular expression + fn try_from(s: String) -> Result { + Regex::new(&s) + } +} + /// Core regular expression methods. impl Regex { /// Compiles a regular expression. Once compiled, it can be used repeatedly