//! returns: 0 //! Standalone methods coexisting with trait instances on the same type. record Widget: Copy { x: i32, y: i32, } // Standalone method. unsafe fn (w: *unsafe Widget) area() -> i32 { return w.x * w.y; } // Trait with its own method. trait Printable { unsafe fn (*unsafe Printable) code() -> i32; } instance Printable for Widget { unsafe fn (w: *unsafe Widget) code() -> i32 { return w.x + w.y; } } /// A second implementation with a distinct method symbol and v-table. record Counter: Copy { /// Value returned through dynamic dispatch. value: i32, } /// Preserve type-qualified method names for a shared trait. instance Printable for Counter { /// Read the counter through its trait receiver. unsafe fn (counter: *unsafe Counter) code() -> i32 { return counter.value; } } @default unsafe fn main() -> i32 { let w = Widget { x: 3, y: 5 }; // Standalone method call. assert w.area() == 15; // Trait method call via trait object. let p: *unsafe opaque Printable = &w; assert p.code() == 8; let counter = Counter { value: 37 }; let other: *unsafe opaque Printable = &counter; assert other.code() == 37; assert p.code() == 8; return 0; }