Let's go with the simplest definitions of affine and linear types: affine meaning value of such type can be used at most once and linear meaning value of such type must be used exactly once.
Question: Can I create a non-linear type that can be used at most N times where N is an usize?
Use case: (roughly speaking)
Note: Zeroize
is a trait that zeros out a value when it is dropped. MEC
stands for 'max exposed count' and EC
stands for 'exposed count'.
Objectives:
- At compile time, if a
Secret
ifMEC
==EC
, then it is not possible to callexpose_secret(...) -> &T
. Perhaps using the typestate pattern.
Draft Implementation:
struct Secret<T: ?Sized + Zeroize, const MEC: usize, const EC: usize=0> { /* private fields */ }