4 ms·
OK, gotta jump in here and ask the dumb question. What's the deal with this thing: lazy_static! { static ref msg_break : TwoWaySearcher<'static> =
by ajross 8y ago
OK, gotta jump in here and ask the dumb question. What's the deal with this thing:
lazy_static! {
static ref msg_break : TwoWaySearcher<'static> = {
TwoWaySearcher::new("\r\n\r\n".as_bytes())
};
}
AFAICT all the code is doing is parsing the input on a double newline (e.g. the equivalent of strstr(input, "\r\n")). And this five-line expression is defining the equivalent of the "\r\n" literal.
What is it that requires that mess? I mean, I get that sometimes typesafety requires some loss of concision, but...
- est31 8y agoThe lazy_static macro (provided by a third party crate) allows you to create static (global, basically singleton pattern) variables that get initialized on first use and then are being reused over and over again. The "lazy ref" is part of that macro's syntax. "msg_break" is the name of the static variable. Then, in Rust, all static things must have type annotations. In this case, it's the "TwoWaySearcher<'static>". The "TwoWaySearcher::new("\r\n\r\n".as_bytes())" is the initializer code that's being only called once. The as_bytes function is solely a type casting operation to convert from a str for which the typesystem guarantees that it's utf-8 formatted to a byte slice that just represents arbitrary data.
- ajross 8y agoActually I sort of knew all that (though it was the first time I'd seen lazy_static! and had to infer it from context). The question was more about "why"? Again in C the equivalent is literally a string literal. There's really no easier way to express "double newline" in Rust than this thing which requires that the reader parse a "lazy static", a parametrized type, an explicit type conversion and a library utility? Why not?
- est31 8y agoThis thing is so bulky because the API that TwoWaySearcher has is so bulky. You could also have a very simple API that just takes a string literal. It's less a problem of Rust and more of that TwoWaySearcher struct.
- coldtea 8y ago>There's really no easier way to express "double newline" in Rust than this thing which requires that the reader parse a "lazy static", a parametrized type, an explicit type conversion and a library utility? Why not? The code doesn't express 'double newline'. That would be just the "\r\n\r\n".as_bytes() The code lazily initializes an TwoWaySearcher to parse such double newlines.
- steveklabnik 8y agoAlso note that b”” is nicer than .as_bytes()
- dbaupp 8y agoOne reason why someone might choose to do that is performance: it ensures that any precomputation required by the two-way algorithm is cached and only done once. After the first search, later ones can jump straight into the main search loop, rather than having to redo any set-up for the fixed needle "\r\n\r\n" (for two-way searcher, it's O(size of needle)). If that setup-performance doesn't matter one can use https://crates.io/crates/twoway https://crates.io/crates/twoway to write: twoway::find_bytes(&buffer[to_scan..], b"\r\n\r\n") at the place where the search happens and skip all the caching effort.