I just stumbled over the implementation of http::HeaderValue::from_static().
In the docstring it is asserted, that the function will not perform any copying. Is that correct?
By calling is_visible_ascii() on each byte the respective byte is moved into the function and since u8 is copy, it will be copied there, right?
If the function indeed performs such copying, my question is, why it is not written more simplistic, like:
if src.as_bytes().iter().any(|byte| !is_visible_ascii(*byte)) {
panic!("invalid bytes in string");
}
Did I miss something or is it worth drafting a PR here?
As for the first question - yes, technically, function will copy bytes to check them, but this is usually treated as insignificant. What's important is that it won't copy them into the constructed HeaderValue - it will reuse the passed-in reference.
Note that ultimately, you have to move data around in order to stand a chance of operating on it. There is no possible function that performs absolutely zero copying, because you have to look at the data in order to do stuff with it. It's simple as that.
What the documentation means is that due to the forever-validity of the &'static [u8] argument, there's no need to assume ownership and copy the bytes at once into an owned (possibly heap-allocated) buffer. For small values such as HTTP headers, the cost of a dynamic allocation would very likely dominate the cost of copying per se, so the wording is perhaps unfortunate/misleading.