Use lazy_static! to avoid expensive regex compiles in loop
This commit is contained in:
parent
3ab9cedbcd
commit
bf84433d5b
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -224,6 +224,7 @@ name = "hexland-server"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
|
"lazy_static",
|
||||||
"rand",
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"sha2",
|
"sha2",
|
||||||
@ -264,6 +265,12 @@ version = "1.0.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
|
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.133"
|
version = "0.2.133"
|
||||||
|
@ -12,3 +12,4 @@ regex = "1.6.0"
|
|||||||
tokio = { version = "1.21.2", features = ["full"] }
|
tokio = { version = "1.21.2", features = ["full"] }
|
||||||
tokio-tungstenite = "0.17.2"
|
tokio-tungstenite = "0.17.2"
|
||||||
futures = "0.3.24"
|
futures = "0.3.24"
|
||||||
|
lazy_static = "1.4.0"
|
@ -6,6 +6,7 @@ use tokio_tungstenite::{
|
|||||||
tungstenite::Message as WsMessage,
|
tungstenite::Message as WsMessage,
|
||||||
};
|
};
|
||||||
use futures::{StreamExt, SinkExt};
|
use futures::{StreamExt, SinkExt};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
pub struct Message {
|
pub struct Message {
|
||||||
@ -25,8 +26,10 @@ pub enum Error {
|
|||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
pub fn parse(text: String) -> Result<Message> {
|
pub fn parse(text: String) -> Result<Message> {
|
||||||
let re = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap();
|
lazy_static! {
|
||||||
match re.captures(text.as_str()) {
|
static ref RE: regex::Regex = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap();
|
||||||
|
}
|
||||||
|
match RE.captures(text.as_str()) {
|
||||||
Some(captures) => {
|
Some(captures) => {
|
||||||
if captures.len() < 3 {
|
if captures.len() < 3 {
|
||||||
Err(Error::BadParse)
|
Err(Error::BadParse)
|
||||||
|
Loading…
Reference in New Issue
Block a user