Expand description
Derive macros for RTC Interceptor trait.
This crate provides two macros that work together:
#[derive(Interceptor)]- Marks a struct as an interceptor and identifies the next field#[interceptor]- Attribute macro for impl blocks to generate trait implementations
§Design Pattern
The examples below are illustrative rather than compiled: the macros only expand to something
meaningful in the presence of the Interceptor trait from
rtc-interceptor, which depends on this crate — so a
doctest here cannot import it. They are exercised for real by rtc-interceptor’s own
documentation and tests.
The design follows Rust’s derive pattern (similar to #[derive(Default)] with #[default]):
use rtc_interceptor::{Interceptor, StreamInfo, TaggedPacket, interceptor};
use rtc_shared::error::Error;
use sansio::Protocol;
use std::collections::VecDeque;
#[derive(Interceptor)]
pub struct MyInterceptor<P: Interceptor> {
#[next]
next: P, // The next interceptor in the chain (can use any field name)
buffer: VecDeque<TaggedPacket>,
}
#[interceptor]
impl<P: Interceptor> MyInterceptor<P> {
#[overrides]
fn handle_read(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
// Custom logic here
self.next.handle_read(msg)
}
}§Pure Delegation (No Custom Logic)
For interceptors that just pass through without modification:
#[derive(Interceptor)]
pub struct PassthroughInterceptor<P: Interceptor> {
#[next]
next: P,
}
#[interceptor]
impl<P: Interceptor> PassthroughInterceptor<P> {}
// Empty impl block - all methods are auto-generated§Required Imports
The macros require certain types to be in scope:
The generated code names sansio::Protocol, Error, StreamInfo and
TaggedPacket, so all four must be in scope at the use site — not only the macros
themselves:
use rtc_interceptor::{Interceptor, StreamInfo, TaggedPacket, interceptor};
use rtc_shared::error::Error;
use sansio::Protocol;Through the rtc umbrella crate the same imports are:
use rtc::interceptor::{Interceptor, StreamInfo, TaggedPacket, interceptor};
use rtc::sansio::Protocol;
use rtc::shared::error::Error;Attribute Macros§
- interceptor
- Attribute macro for impl blocks to generate Protocol and Interceptor implementations.
Derive Macros§
- Interceptor
- Derive macro that marks a struct as an interceptor.