Kernel Error Codes as Rust Results
C kernel code reports failure by returning a small negative number such as minus twenty-two for invalid argument, and a famous class of bugs is simply forgetting to check it. Rust in the kernel wraps those numbers in an Error type and returns Result, so the compiler complains if you ignore the outcome, and the question mark operator lets a failing call exit your function with the right code in one character. Helper functions convert in both directions at the C boundary: one turns a returned negative number into a Result, the other turns your Result back into the number C expects. This is how a Rust driver stays a well-behaved kernel citizen while making unchecked failures hard to write.
Questions this Concept answers
- Why is returning `Result` an improvement over returning a negative error number the way C does?
This Concept is waiting for its first lesson!
C kernel code reports failure by returning a small negative number such as minus twenty-two for invalid argument, and a famous class of bugs is simply forgetting to check it. Rust in the kernel wraps those numbers in an Error type and returns Result, so the compiler complains if you ignore the outcome, and the question mark operator lets a failing call exit your function with the right code in one character. Helper functions convert in both directions at the C boundary: one turns a returned negative number into a Result, the other turns your Result back into the number C expects. This is how a Rust driver stays a well-behaved kernel citizen while making unchecked failures hard to write.
Are you a teacher? Sign in to start contributing.
Sign In