pub struct Topic<T: Message> { /* private fields */ }Expand description
A type-safe topic with an associated message type
This is the core abstraction that enables compile-time type safety for pub/sub. Topics are defined as constants and carry type information via phantom types.
§Type Safety Guarantee
The compiler ensures that:
- Subscribe returns the correct message type
- Publish accepts only the correct message type
- No runtime type confusion is possible
§Example: Defining Topics
use mecha10::topics::Topic;
use mecha10::messages::Image;
// Simple topic definition
pub const CAMERA_RGB: Topic<Image> = Topic::new("/sensor/camera/rgb");
// Custom message type
#[derive(Serialize, Deserialize)]
struct MyMessage {
value: f32,
}
pub const CUSTOM: Topic<MyMessage> = Topic::new("/custom/topic");§Example: Using Topics
// Type is inferred as Receiver<Image>
let mut images = ctx.subscribe(sensor::CAMERA_RGB).await?;
while let Some(image) = images.recv().await {
// image is type Image
println!("Image: {}x{}", image.width, image.height);
// Compiler ensures we publish the right type
ctx.publish_to(sensor::CAMERA_RGB, &image).await?;
// This would be a compile error:
// ctx.publish_to(sensor::CAMERA_RGB, &laser_scan).await?;
// ^^^^^^^^^^^ expected Image, found LaserScan
}Implementations§
Trait Implementations§
impl<T: Message> Copy for Topic<T>
Auto Trait Implementations§
impl<T> Freeze for Topic<T>
impl<T> RefUnwindSafe for Topic<T>where
T: RefUnwindSafe,
impl<T> Send for Topic<T>
impl<T> Sync for Topic<T>
impl<T> Unpin for Topic<T>where
T: Unpin,
impl<T> UnwindSafe for Topic<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.