interface Repr
Level 1 (§9.5): the type is serialized as another type W in every format and schema.
Example:
use std.ser { Repr, encode, decode, hex, EncodeError, DecodeError }
@repr(i64)
data Money { minor: i64 }
impl Repr<i64> for Money {
fn toRepr() -> i64 => minor * 100
static fn fromRepr(v: i64) -> Money | DecodeError {
if v < 0 { fail DecodeError.Invalid(msg: "negative amount") }
Money{ minor: v / 100 }
}
}
fn main() io {
let b = encode(&Money{ minor: 7 })!
print("{hex(&b)} {decode<Money>(&b)!.minor}")
}Вывод
08bc05 7
pub(api) interface Repr<W>С версии. 1.0.0-beta
Методы
toRepr | () -> WThe value as its representation W; called when the type is encoded. |
fromRepr | (v: W) -> Self | DecodeErrorThe value from its representation; called when the type is decoded. |
Определено в: std/ser/ser.oxl:159