Ben Chuanlong Du's Blog

It is never too late to learn.

Constraints on Types in Rust

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Ways to Make Sure that a Type in Rust Satisfy Certain Conditions

  1. type bounds using the where clause

  2. disable users from constructing instances of a struct and provide initialized instances (with conditions satisfied) for users to use

  3. use assert to make sure that parameters passed to construction methods satisfy required conditions

  4. make construction methods return instances satisfying required conditions

  5. Define a sealed trait which representing the required conditions and define types implementing the sealed trait. Make the struct take a generic parameter implementing the sealed trait.

Libraries on Type Constraints

nutype

Nutype embraces the simple idea: the type system can be leveraged to track the fact that something was done, so there is no need to do it again. If a piece of data was once sanitized and validated we can rely on the types instead of sanitizing and validating again and again when we're in doubt.

validator

Validator provides custom derive to simplify struct validation inspired by marshmallow and Django validators.

References

Comments