I'm reading the source code of actix-web-httpauth. and I'm feeling confused at the strange code, maybe i don't know the usage:
Now I put the code below:
- there are two traits with the same method name parse:
pub trait Header: TryIntoHeaderValue {
/// Returns the name of the header field.
fn name() -> HeaderName;
/// Parse the header from a HTTP message.
fn parse<M: HttpMessage>(msg: &M) -> Result<Self, ParseError>;
}
pub trait Scheme: TryIntoHeaderValue + Debug + Display + Clone + Send + Sync {
/// Try to parse an authentication scheme from the `Authorization` header.
fn parse(header: &HeaderValue) -> Result<Self, ParseError>;
}
- and here is the code confused me:
impl<S: Scheme> Header for Authorization<S> {
#[inline]
fn name() -> HeaderName {
AUTHORIZATION
}
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError> {
let header = msg.headers().get(Self::name()).ok_or(ParseError::Header)?;
let scheme = S::parse(header).map_err(|_| ParseError::Header)?;
Ok(Authorization(scheme))
}
}
- I already know that the
fn parsefrom Header trait, but why the S::parse method, which has no default implementation, could be called directly here?
all the code are extracted from the actix-web-httpauth
I have tried:
- google to find similar situation, but not found
- found several rust books, but no result
- try to simplify the code, and run in the playground, but only get
not all trait items implementederror.
here is the simplified code:
trait TestWithoutDefinition {
fn without_body(input: i32) -> i64;
}
struct Test{}
impl TestWithoutDefinition for Test {}
fn main() {
let input = 11;
Test.without_body(input);
}
I'm expecting the missing knowledge from this question.