Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mod platform {
- // Contains platform-dependent implementations
- // This module must be kept private
- mod platform_impl {
- pub struct Context();
- impl Context {
- pub fn new() -> Context {
- Context()
- }
- }
- pub struct Window();
- impl Window {
- pub fn new(_context: &Context) -> Window {
- // using context to construct a window
- Window()
- }
- pub fn println(&self) {
- println!("Example text")
- }
- }
- }
- pub fn init<T: FnOnce(&PlatformContext)>(f: T) {
- let ctx = PlatformContext {
- inner: platform_impl::Context::new()
- };
- f(&ctx);
- }
- pub struct PlatformContext {
- inner: platform_impl::Context
- }
- impl PlatformContext {
- pub fn window(&self) -> Window {
- Window {
- inner: platform_impl::Window::new(&self.inner)
- }
- }
- }
- // I would like to put Window in its own module while keeping
- // platform_impl private, and still being constructed by PlatformContext
- pub struct Window {
- inner: platform_impl::Window
- }
- impl Window {
- pub fn println(&self) {
- self.inner.println()
- }
- }
- }
- pub fn main() {
- platform::init(|ctx| {
- let window = ctx.window();
- window.println();
- })
- }
Add Comment
Please, Sign In to add comment