Type safety
Newtypes provide static distinctions (C-NEWTYPE)
Newtypes can statically distinguish between different interpretations of an underlying type.
For example, a f64 value might be used to represent a quantity in miles or in
kilometers. Using newtypes, we can keep track of the intended interpretation:
;
;
Once we have separated these two types, we can statically ensure that we do not confuse them. For example, the function
cannot accidentally be called with a Kilometers value. The compiler will
remind us to perform the conversion, thus averting certain catastrophic bugs.
Arguments convey meaning through types, not bool or Option (C-CUSTOM-TYPE)
Prefer
let w = new
over
let w = new
Core types like bool, u8 and Option have many possible interpretations.
Use a deliberate type (whether enum, struct, or tuple) to convey interpretation
and invariants. In the above example, it is not immediately clear what true
and false are conveying without looking up the argument names, but Small and
Round are more suggestive.
Using custom types makes it easier to expand the options later on, for example
by adding an ExtraLarge variant.
See the newtype pattern (C-NEWTYPE) for a no-cost way to wrap existing types with a distinguished name.
Types for a set of flags are bitflags, not enums (C-BITFLAG)
Rust supports enum types with explicitly specified discriminants:
Custom discriminants are useful when an enum type needs to be serialized to an
integer value compatibly with some other system/language. They support
“typesafe” APIs: by taking a Color, rather than an integer, a function is
guaranteed to get well-formed inputs, even if it later views those inputs as
integers.
An enum allows an API to request exactly one choice from among many. Sometimes
an API’s input is instead the presence or absence of a set of flags. In C code,
this is often done by having each flag correspond to a particular bit, allowing
a single integer to represent, say, 32 or 64 flags. Rust’s bitflags crate
provides a typesafe representation of this pattern.
use bitflags;
bitflags!
Builders enable construction of complex values (C-BUILDER)
Some data structures are complicated to construct, due to their construction needing:
- a large number of inputs
- compound data (e.g. slices)
- optional configuration data
- choice between several flavors
which can easily lead to a large number of distinct constructors with many arguments each.
If T is such a data structure, consider introducing a T builder:
- Introduce a separate data type
TBuilderfor incrementally configuring aTvalue. When possible, choose a better name: e.g.Commandis the builder for a child process,Urlcan be created from aParseOptions. - The builder constructor should take as parameters only the data required to
make a
T. - The builder should offer a suite of convenient methods for configuration,
including setting up compound inputs (like slices) incrementally. These
methods should return
selfto allow chaining. - The builder should provide one or more “terminal” methods for actually
building a
T.
The builder pattern is especially appropriate when building a T involves side
effects, such as spawning a task or launching a process.
In Rust, there are two variants of the builder pattern, differing in the treatment of ownership, as described below.
Non-consuming builders (preferred)
In some cases, constructing the final T does not require the builder itself to
be consumed. The following variant on std::process::Command is one example:
// NOTE: the actual Command API does not use owned Strings;
// this is a simplified version.
Note that the spawn method, which actually uses the builder configuration to
spawn a process, takes the builder by shared reference. This is possible because
spawning the process does not require ownership of the configuration data.
Because the terminal spawn method only needs a reference, the configuration
methods take and return a mutable borrow of self.
The benefit
By using borrows throughout, Command can be used conveniently for both
one-liner and more complex constructions:
// One-liners
new.arg.spawn;
// Complex configuration
let mut cmd = new;
if size_sorted
cmd.arg;
cmd.spawn;
Consuming builders
Sometimes builders must transfer ownership when constructing the final type T,
meaning that the terminal methods must take self rather than &self.
Here, the stdout configuration involves passing ownership of an io::Write,
which must be transferred to the task upon construction (in spawn).
When the terminal methods of the builder require ownership, there is a basic tradeoff:
-
If the other builder methods take/return a mutable borrow, the complex configuration case will work well, but one-liner configuration becomes impossible.
-
If the other builder methods take/return an owned
self, one-liners continue to work well but complex configuration is less convenient.
Under the rubric of making easy things easy and hard things possible, all
builder methods for a consuming builder should take and return an owned
self. Then client code works as follows:
// One-liners
new.spawn;
// Complex configuration
let mut task = new;
task = task.named; // must re-assign to retain ownership
if reroute
task.spawn;
One-liners work as before, because ownership is threaded through each of the
builder methods until being consumed by spawn. Complex configuration, however,
is more verbose: it requires re-assigning the builder at each step.