Files
sx/examples/90-protocol-real-pointer-return.sx
2026-05-18 17:40:10 +03:00

31 lines
644 B
Plaintext

// A protocol method declared with a real pointer return (`-> *u8`,
// NOT `-> Self`) returns the raw pointer to the caller without the
// dispatch path auto-dereferencing it. Without this, a method whose
// pointee is a single byte gets `sizeof(target)` bytes loaded past
// it and segfaults.
#import "modules/std.sx";
Proto :: protocol {
get :: () -> *u8;
}
Impl :: struct {
val: u8 = 42;
}
impl Proto for Impl {
get :: (self: *Impl) -> *u8 {
@self.val;
}
}
main :: () -> s32 {
imp : Impl = .{};
p : Proto = xx @imp;
raw : *u8 = p.get();
addr_word : u64 = xx raw;
print("got pointer: {}\n", addr_word != 0);
0;
}