Check server and client communication

use std::{io::*, net::*};
fn server(mut t: TcpStream, a: &[u8]) {
    t.write_all(&a[..=8]).unwrap()
}
fn client(mut t: TcpStream, a: &mut [u8]) {
    t.read_exact(&mut a[..8]).unwrap()
}
fn main() {}

Compiler can't check the logic of server and client, e.g, any wrong slice sizes, the inclusive ..=8 and exclusive ..8 range for webserver and client.

How would compiler check it? In your example those two functions don't even interact with each other.

This is usually solved by using some serialization library.

At minimum you could use byteorder to read individual numbers (I assume that 8 is for u64).

You could also make the messages use MessagePack, bincode, protobuf or something else, and have the compiler generate correct serialisation and deserialisation code automatically.

2 Likes