pub trait Table: Sized {
// Required methods
fn new() -> Self;
fn read(reader: &mut dyn Read) -> Result<Self>;
fn write(&self, writer: &mut dyn Write) -> Result<()>;
// Provided method
fn read_packet(
reader: &mut dyn Read,
prefix: &'static [u8; 4],
) -> Result<Packet<Self>> { ... }
}
Expand description
A UTF table that can be read, written, and constructed from nothing
Required Methods§
Sourcefn new() -> Self
fn new() -> Self
Creates a new table with default constant values and no rows
§Example
#[utf_table]
struct Tab {
#[constant]
constant: i32,
row_value: i64,
}
fn main() {
let table = Tab::new();
assert_eq!(table.constants.constant, 0);
assert_eq!(table.rows.len(), 0);
}
Sourcefn read(reader: &mut dyn Read) -> Result<Self>
fn read(reader: &mut dyn Read) -> Result<Self>
Reads a table from the given stream
If the table is malformed, or if the table’s schema does not match this type, then this function will fail.
§Example
#[utf_table]
struct Tab {
#[constant]
constant: i32,
row_value: i64,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut file = File::open("table.bin")?;
let table = Tab::read(&mut file)?;
// ... do something ...
Ok(())
}
Sourcefn write(&self, writer: &mut dyn Write) -> Result<()>
fn write(&self, writer: &mut dyn Write) -> Result<()>
Writes a table to the given stream
§Example
#[utf_table]
struct Tab {
#[constant]
constant: i32,
row_value: i64,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut file = File::create("table.bin")?;
let table = Tab::new();
// ... do something ...
table.write(&mut file)?;
Ok(())
}
Provided Methods§
Sourcefn read_packet(
reader: &mut dyn Read,
prefix: &'static [u8; 4],
) -> Result<Packet<Self>>
fn read_packet( reader: &mut dyn Read, prefix: &'static [u8; 4], ) -> Result<Packet<Self>>
Reads a UTF table packet from the given stream, verifying that it has the given 4-byte prefix.
§Example
#[utf_table]
struct Tab {
#[constant]
constant: i32,
row_value: i64,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut file = File::open("table.bin")?;
let table: Packet<Tab> = Tab::read_packet(&mut file, b"TAB ")?;
// ... do something ...
Ok(())
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.