This does not work:
trait UnderlyingBuffer {
read_u8(Self, Int) -> Byte?
read_u16_be(Self, Int) -> Int?
}
impl UnderlyingBuffer for Bytes with read_u8(self, idx : Int) -> Byte? {
if idx < 0 || idx >= self.length() {
return None
}
Some(self[idx])
}
impl UnderlyingBuffer for Bytes with read_u16_be(self, idx : Int) -> Int? {
let b1 = self.read_u8(idx)? // E4015: Type Bytes has no method read_u8
let b2 = self.read_u8(idx + 1)? // E4015: Type Bytes has no method read_u8
b1.to_int().lsl(8).lor(b2.to_int())
}
Neither does this:
fn UnderlyingBuffer::read_u8(self : Bytes, idx : Int) -> Byte? {
if idx < 0 || idx >= self.length() {
return None
}
Some(self[idx])
}
fn UnderlyingBuffer::read_u16_be(self : Bytes, idx : Int) -> Int? {
let b1 = self.read_u8(idx)? // E4015: Type Bytes has no method read_u8
let b2 = self.read_u8(idx + 1)? // E4015: Type Bytes has no method read_u8
b1.to_int().lsl(8).lor(b2.to_int())
}
While this, umm… it’s an ICE
impl UnderlyingBuffer for Bytes
fn read_u8(self : Bytes, idx : Int) -> Byte? {
if idx < 0 || idx >= self.length() {
return None
}
Some(self[idx])
}
fn read_u16_be(self : Bytes, idx : Int) -> Int? {
let b1 = self.read_u8(idx)?
let b2 = self.read_u8(idx + 1)?
b1.to_int().lsl(8).lor(b2.to_int())
}
failed: moonc check -error-format json [...] -target wasm-gc
Fatal error: exception File "lib/xml/typing/toplevel_typer.ml", line 1343, characters 32-38: Assertion failed
error: failed when checking
This is the only way I found for it to work, but it’s too verbose IMHO:
fn UnderlyingBuffer::read_u16_be(self : Bytes, idx : Int) -> Int? {
let b1 = UnderlyingBuffer::read_u8(self, idx)?
let b2 = UnderlyingBuffer::read_u8(self, idx + 1)?
Some(b1.to_int().lsl(8).lor(b2.to_int()))
}