expression

Track a partial native write without losing bytes

A write can accept fewer bytes than requested. The caller must retain the unsent suffix; on WouldBlock wait for a later writable event. This dependency-free model simulates two short writes and one WouldBlock rather than relying on kernel buffer sizes. A successful zero-byte write with nonempty input should be handled as WriteZero. Expected stdout (not executed): pending: 3 sent: 5 Common mistake: Resending the whole buffer after a partial write. Run only in the isolated fixture runner, not a public code playground.

Formal Syntax & Structure

rust syntaxrust
offset += written; retry remaining slice after readiness

Working Example

Example Snippetrust
use std::io::{self,ErrorKind};fn main() {    let data=b"hello"; let mut offset=0;    let attempts=[Ok(2usize), Err(io::Error::from(ErrorKind::WouldBlock)), Ok(3)];    for result in attempts {        match result {            Ok(n) if n>0 && n<=data.len()-offset => offset+=n,            Err(e) if e.kind()==ErrorKind::WouldBlock => println!("pending: {}", data.len()-offset),            _ => panic!("unexpected simulated write"),        }    }    println!("sent: {offset}");}
Rust Reference