← all posts

Skipping the driver stack with a tiny Rust crate

RustHardware

Label and receipt printers already speak a perfectly good language. It’s called ZPL, or ESC/POS, and the printer understands it natively. The tragedy is how much software we stack on top of that just to avoid saying it out loud.

The task was supposed to be simple: print a shipping label from a backend service. The “proper” path was a print spooler, a vendor driver, a rasterizer, and a queue — a whole subsystem whose job was to turn my label back into the exact bytes the printer already knew how to accept. I was generating ZPL. The printer wanted ZPL. Everything in between was ceremony.

The printer is just a file

On Linux, a USB printer shows up as a character device. You can write to it the same way you write to any file:

use std::fs::OpenOptions;
use std::io::Write;

let zpl = b"^XA^FO50,50^ADN,36,20^FDHello^FS^XZ";

let mut printer = OpenOptions::new()
    .write(true)
    .open("/dev/usb/lp0")?;

printer.write_all(zpl)?;

That’s the whole trick. The label came out, sharp and instant, with no driver installed and no spooler running. The first time it worked I genuinely laughed — I’d spent an afternoon reading CUPS backend documentation for something that was four lines of file I/O.

Why bother with a crate, then?

Because “write bytes to a path” is the easy 80%. The annoying 20% is everything platform-specific:

  • On Windows there is no /dev/usb/lp0; you open a printer share or a raw USB handle through the Win32 spooler in passthrough mode.
  • Network printers want a raw TCP socket on port 9100, not a file at all.
  • Permissions, device naming, and “which lp is my printer this week” all vary.

Those are exactly the details you want to write once and never think about again. So raw-printer presents a single idea — a writable connection to a device — and hides where that device lives:

let printer = Printer::new(Interface::Usb("/dev/usb/lp0"))?;
printer.write(&zpl)?;

// or, without changing the calling code:
let printer = Printer::new(Interface::Network("192.168.1.50:9100"))?;
printer.write(&zpl)?;

The command bytes are yours to build — the crate is deliberately dumb about ZPL versus ESC/POS versus anything else. It moves bytes to hardware and gets out of the way. That’s the entire value proposition.

The lesson that keeps generalizing

Before you adopt a layer, check whether the thing underneath already speaks your language.

Drivers exist for real reasons — color management, rasterizing arbitrary documents, sharing one printer across an office. But when you are a backend emitting device-native commands to a device that wants device-native commands, every layer in between is just latency and a new place for things to break.

I’ve since caught myself making the same move elsewhere: talking to an S3-compatible store with raw signed requests instead of a heavy SDK, or hitting a database over its wire protocol instead of an ORM I didn’t need. Not always the right call. But “what does the layer below actually accept?” is a question that has saved me more time than almost any other.

The crate is on crates.io. It is under 300 lines. That is the point.

— Abu more posts →