//! returns: 0 //! Test trait dispatch inside control flow. //! //! Exercises: trait method calls inside loops and conditionals, //! verifying that vtable dispatch works correctly when interleaved //! with branching and iteration. record Counter: Copy { value: i32, } trait Stepper { unsafe fn (*unsafe mut Stepper) step() -> i32; unsafe fn (*unsafe Stepper) current() -> i32; } instance Stepper for Counter { unsafe fn (c: *unsafe mut Counter) step() -> i32 { set c.value = c.value + 1; return c.value; } unsafe fn (c: *unsafe Counter) current() -> i32 { return c.value; } } @default unsafe fn main() -> i32 { let mut c = Counter { value: 0 }; let s: *unsafe mut opaque Stepper = &mut c; // Dispatch in a while loop. let mut i: i32 = 0; while i < 5 { s.step(); set i = i + 1; } assert s.current() == 5; // Dispatch in a conditional. if s.current() > 3 { s.step(); } assert s.current() == 6; // Dispatch result used as loop condition. while s.current() < 10 { s.step(); } assert s.current() == 10; // Dispatch result used in conditional expression. let v = s.current(); assert v == 10; return 0; }