Rust에서 사용 된 macro의 구현이 궁금할 때 cargo expand를 사용할 수 있습니다.

설치

$ cargo install cargo-expand
   

사용법

이전 글에서 사용한 코드를 expand해 봅시다. cargo expand 시 expand 된 코드가 stdout으로 출력됩니다.

$ cargo expand --bin actix-and-tonic

이 예제는 하나의 파일로 이루어져 있어 binary를 지정했지만, 특정 module 코드를 expand하기 위해서 module을 지정할 수도 있습니다.

$ cargo expand path::to:module

Message 구현에 사용했던 Message macro는

1
2
3
#[derive(Default, Message)]
#[rtype(result = "String")]
struct GetName;

이렇게 풀어쓸 수 있습니다.

1
2
3
4
5
6
7
8
9
impl ::core::default::Default for GetName {
    #[inline]
    fn default() -> GetName {
        GetName {}
    }
}
impl ::actix::Message for GetName {
    type Result = String;
}

derive(Default)에 의해 Default 구현이 추가되었고, derive(Message)에 의해 Message 구현이 추가 되었습니다. rtype(result="String")은 그대로 type Result = String으로 변환됨을 확인할 수 있습니다.