Help on implementing subclasses

⚓ Rust    📅 2026-05-19    👤 surdeus    👁️ 3      

surdeus

I currently have a trait named Class. It needs to be dyn-compatible, but it also needs to return an optional subclass. The signature of the function as I have it set up now is fn(&self) -> Option<Box<dyn Subclass<Self>>> so as to not let other classes subclasses apply to one they shouldn't.

The issue is, because since it references Self in it's return type, it's no longer dyn-compatible. Is there a way, either modifying my implementation or providing another one, to go about this? I'd like to mention that Self: !Sized.

Thanks!
- Clover B.-P. Johnson

2 posts - 2 participants

Read full topic

🏷️ Rust_feed