Ben Chuanlong Du's Blog

It is never too late to learn.

Const Generic in Rust

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

  1. Starting from Rust 1.51, constant generics is supported for integral types.

  2. The crate static_assertions can be used to assert that a const generic parameter satisfy certain conditions at compile time. This is an alternative to const bounds using where before feature(generic-const-exprs) is stablized.

Disable Users From Constructing Instances of a Public Struct

  1. Make sure the struct has at least one private field so that users cannot construct instances directly. If all fields of a struct are public, just add a dumpy private filed into the struct.
  2. Do not privede construction methods or make construction methods as private.

Sealed Trait

A definitive guide to sealed traits in Rust

To disable a trait to be implemented by downstream users, simply use a marker trait which is not visible to downstream users.

https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed

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

Please refer to Constraints on Types in Rust for detailed discussions.

References

Comments