Why the trait method without default implementation could be called directly here?

Viewed 31

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:

  1. 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>;
}
  1. 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))
    }
}
  1. I already know that the fn parse from 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:

  1. google to find similar situation, but not found
  2. found several rust books, but no result
  3. try to simplify the code, and run in the playground, but only get not all trait items implemented error.

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.

0 Answers
Related