Open yoshuawuyts opened 3 years ago
Sure!
Would you like me to use from_ascii(..).unwrap()
or unsafe { from_ascii_unchecked(..)}
It seems like both would work equally well but the second preserves unsafe
.
Oh, good question. Yeah, I actually care less about the perf here than I care about removing unsafe
. I think the former is probably desirable.
If we panic on improper ascii, doesn't that mean a http client could shut down the server?
We are already calling ensure!
before the unsafe code. I'm not aware of the specifics of the implementation of ensure
but I think it will panic in some form. The unwrap
panicking should be impossible because we are already ensuring it is ASCII beforehand.
Should the behavior of HeaderName::from_lowecase_str
be to panic, be unsafe, or to return a result? Currently it assumes the given str
is valid ASCII and is lowercase.
So the question for HeaderName::from_lowecase_str
is how to make it work with existing usages. We can use an unsafe {AsciiStr::from_ascii_unchecked(s)}
but this function is not const
which we need for HeaderName::from_lowercase_str
to be const
, which we need to be able to create const
s in src/headers/constants.rs
.
The only solution I can think of is to use lazy_static for the constants, thus allowing the use of non-const functions in their initialization, thus allowing us to use unsafe {AsciiStr::from_ascii_unchecked(s)}
in HeaderName::from_lowercase_str
.
@jbr @yoshuawuyts
@yoshuawuyts I'm going to "unassign" myself from this one, because it requires multiple high-level design decisions.
We could remove almost every instance of
unsafe
in our codebase by replacing the use ofString
inHeaderName
andHeaderValue
withascii::AsciiString
instead. Implementing this shouldn't be too hard, but code-wise it's a fair few lines that need changing.This wouldn't change anything in our API signatures either, assuming we don't have any bugs in how we handle ASCII today. @brightly-salty would you be interested in perhaps trying to tackle this?