[−][src]Module index_ext::tag
Not quite dependent typing for eliding bounds checks.
Lifetime generativity
The main idea is to use lifetimes as a compile time tag to identify a particular exact slice
without keeping a direct reference of it. This means that you can not choose any of the
lifetime parameters that you see in this module. Instead, you must be prepared to handle
arbitrary lifetime which will make it opaque to you and the compiler. Each lifetime guarantees
that all Ref
and Mut
with that exact lifetime are at least as long as all the sizes in
Len
structs of the same lifetime and each Idx
is bounded by some Len
. While this may seem
very restrictive at first, it still allows you to pass information on a slice's length across
function boundaries by explicitly mentioning the same lifetime twice. Additionally you're
allowed some mutable operations on indices that can not exceed the original bounds.
Use with_ref
and with_mut
as the main entry constructors.
Checked constant bounds
Alternatively we can choose other unique type instances. By that we mean that for any particular
type exactly one value must be used to construct ExactSize
. One possible way is if this is
simply a constant which is implemented by the Constant
wrapper and its ConstantSource
trait. For example one may define:
use index_ext::tag::{Constant, ConstantSource, ExactSize}; const BUFFER_SIZE: usize = 4096; struct BufferSize4K; impl ConstantSource for BufferSize4K { const LEN: usize = BUFFER_SIZE; } const LEN: ExactSize<Constant<BufferSize4K>> = Constant::EXACT_SIZE;
Named bounds
And finally one might come up with an internal naming scheme where types are used to express
unique bounds. This requires some unsafe code and the programmers guarantee of uniqueness of
values but permits the combination of runtime values with 'static
lifetime.
Structs
Boxed | An owned, allocated slice with a checked length. |
Constant | A tag using a |
ExactSize | The exact length separating slices and indices for a tag. |
Generative | A generative lifetime. |
Idx | A valid index for all slices of the same length. |
Len | The length of a particular slice (or a number of slices). |
Mut | A mutable slice with a unique lifetime. |
Named | A named unique tag. |
NonZeroLen | The length of a non-empty slice. |
Ref | A slice with a unique lifetime. |
Traits
ConstantSource | A type that names a constant buffer size. |
Tag | A type suitable for tagging length, indices, and containers. |
Functions
with_mut | Enter a region for soundly indexing a mutable slice without bounds checks. |
with_ref | Enter a region for soundly indexing a slice without bounds checks. |