Guest User

Untitled

a guest
Jun 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. mod platform {
  2. // Contains platform-dependent implementations
  3. // This module must be kept private
  4. mod platform_impl {
  5. pub struct Context();
  6. impl Context {
  7. pub fn new() -> Context {
  8. Context()
  9. }
  10. }
  11.  
  12. pub struct Window();
  13. impl Window {
  14. pub fn new(_context: &Context) -> Window {
  15. // using context to construct a window
  16. Window()
  17. }
  18.  
  19. pub fn println(&self) {
  20. println!("Example text")
  21. }
  22. }
  23. }
  24.  
  25. pub fn init<T: FnOnce(&PlatformContext)>(f: T) {
  26. let ctx = PlatformContext {
  27. inner: platform_impl::Context::new()
  28. };
  29. f(&ctx);
  30. }
  31.  
  32. pub struct PlatformContext {
  33. inner: platform_impl::Context
  34. }
  35. impl PlatformContext {
  36. pub fn window(&self) -> Window {
  37. Window {
  38. inner: platform_impl::Window::new(&self.inner)
  39. }
  40. }
  41. }
  42.  
  43. // I would like to put Window in its own module while keeping
  44. // platform_impl private, and still being constructed by PlatformContext
  45. pub struct Window {
  46. inner: platform_impl::Window
  47. }
  48. impl Window {
  49. pub fn println(&self) {
  50. self.inner.println()
  51. }
  52. }
  53. }
  54.  
  55. pub fn main() {
  56. platform::init(|ctx| {
  57. let window = ctx.window();
  58. window.println();
  59. })
  60. }
Add Comment
Please, Sign In to add comment