@@ -36,27 +36,26 @@ impl Username {
36
36
- The newtype pattern, combined with Rust's module and visibility system, can be
37
37
used to _ guarantee_ that instances of a given type satisfy a set of
38
38
invariants.\
39
- In the example above, the raw ` String ` stored inside the
40
- ` Username ` struct can't be accessed directly from other modules or crates,
41
- since it's not marked as ` pub ` or ` pub(in ...) ` . Consumers of the ` Username `
42
- type are forced to use the ` new ` method to create instances. In turn, ` new `
43
- performs validation, thus ensuring that all instances of ` Username ` satisfy
44
- those checks.
39
+ In the example above, the raw ` String ` stored inside the ` Username ` struct
40
+ can't be accessed directly from other modules or crates, since it's not marked
41
+ as ` pub ` or ` pub(in ...) ` . Consumers of the ` Username ` type are forced to use
42
+ the ` new ` method to create instances. In turn, ` new ` performs validation, thus
43
+ ensuring that all instances of ` Username ` satisfy those checks.
45
44
46
45
- The ` as_str ` method allows consumers to access the raw string representation
47
- (e.g. to store it in a database) but, thanks to Rust's borrow checker,
48
- they can't modify it.
46
+ (e.g. to store it in a database) but, thanks to Rust's borrow checker, they
47
+ can't modify it.
49
48
50
- - Stress the importance of evaluating _ the entire API surface_ exposed by a newtype
51
- to determine if invariants are indeed bullet-proof.\
52
- It is crucial to consider all possible interactions, including trait implementations,
53
- that may allow users to bypass the invariants. For example, if the ` Username `
54
- type implements the ` DerefMut ` trait, users can modify the underlying string
55
- directly, bypassing the validation checks in ` new ` .
49
+ - Stress the importance of evaluating _ the entire API surface_ exposed by a
50
+ newtype to determine if invariants are indeed bullet-proof.\
51
+ It is crucial to consider all possible interactions, including trait
52
+ implementations, that may allow users to bypass the invariants. For example,
53
+ if the ` Username ` type implements the ` DerefMut ` trait, users can modify the
54
+ underlying string directly, bypassing the validation checks in ` new ` .
56
55
57
56
- Type-level invariants have second-order benefits.\
58
- The input is validated once, at the boundary, and the rest of the program can rely
59
- on the invariants being upheld. We can avoid redundant validation and
57
+ The input is validated once, at the boundary, and the rest of the program can
58
+ rely on the invariants being upheld. We can avoid redundant validation and
60
59
"defensive programming" checks throughout the program, reducing noise and
61
60
improving performance.
62
61
0 commit comments