Module kinds

Source
Available on crate feature kinds only.
Expand description

§Container Kind Traits

Currently, Rust doesn’t allow bounds like where C: for<T: Send> Container<T> + Clone + Send + Sync. The solution is to define an extra trait with whatever you need as the bounds of a GAT (generic associated type):

use generic_container::FragileMutContainer;
use dupe::Dupe;

pub trait NeededContainerStuff {
   // Implementors should use `T: ?Sized` when possible. But right now, the only way to create,
   // for example, `Arc<Mutex<[T]>>` is via unsizing coercion from `Arc<Mutex<[T; N]>>`;
   // as such, a `T: ?Sized` bound would be somewhat useless without also requiring the
   // container to support unsizing coercion, which currently requires nightly-only traits.
   type MutableContainer<T: Send>: FragileMutContainer<T> + Dupe + Send + Sync;
}

If some data needs thread-safe mutability, but you don’t want to pay the cost of a lock for read-only data, you can use multiple GATs:

use generic_container::{FragileMutContainer, Container};
use dupe::Dupe;

pub trait NeededContainerStuff {
   // E.g.: `Arc<Mutex<T>>`, or something `parking_lot`-based
   type MutableContainer<T: Send>: FragileMutContainer<T> + Dupe + Send + Sync;
   // E.g.: `Arc<T>`
   type ReadOnlyContainer<T: Send + Sync>: Container<T> + Dupe + Send + Sync;
}

Such a trait is called a “container kind trait” (with implementations being “container kinds”, just as implementations of the container traits are “containers”). Relevant characteristics for a container kind’s container include the eight container traits, Send + Sync bounds (possibly only when T is Send + Sync, or just Send), Dupe, whether the GAT allows T to be unsized, and Debug bounds.

Not every conceivably-useful container kind trait is provided, as there would be exponentially-many such traits. Note also that creating simple container kind traits that can be combined into more complicated bounds does work, but not well. A container kind should set the GAT of each container kind trait it implements to the same container type; this can be asserted or required with Rust’s type system, but the trait solver doesn’t understand it very well. Such container kind traits would likely not be pleasant to use.

When the kinds feature is enabled, container kinds for common sorts of containers are provided. They do not use Dupe bounds, and do not mess with type-equality shenanigans that confuse the trait solver.

Structs§

ArcKind
The container kind corresponding to Arc<T> as a container for T.
ArcMutexKind
The container kind corresponding to Arc<Mutex<T>> as a container for T.
ArcRwLockKind
The container kind corresponding to Arc<RwLock<T>> as a container for T.
ArcThreadCheckedMutexKind
The container kind corresponding to Arc<ThreadCheckedMutex<T>> as a container for T.
BoxKind
The container kind corresponding to Box<T> as a container for T.
CheckedRcRefCellKind
The container kind corresponding to CheckedRcRefCell<T> as a container for T.
RcKind
The container kind corresponding to Rc<T> as a container for T.
RcRefCellKind
The container kind corresponding to Rc<RefCell<T>> as a container for T.
TKind
The container kind corresponding to T as a container for itself.

Traits§

ArcLike
A container kind trait based on how Arc<T> acts as a container for T.
ArcMutexLike
A container kind trait based on how Arc<Mutex<T>> acts as a container for T.
ArcRwLockLike
A container kind trait based on how Arc<RwLock<T>> acts as a container for T.
ArcThreadCheckedMutexLike
A container kind trait based on how Arc<ThreadCheckedMutex<T>> acts as a container for T.
BoxLike
A container kind trait based on how Box<T> acts as a container for T.
CheckedRcRefCellLike
A container kind trait based on how CheckedRcRefCell<T> acts as a container for T.
FragileArcLike
A container kind trait based on how Arc<T> acts as a container for T.
FragileBoxLike
A container kind trait based on how Box<T> acts as a container for T.
FragileRcLike
A container kind trait based on how Rc<T> acts as a container for T.
FragileTLike
A container kind trait based on how a type T acts as a container for itself.
RcLike
A container kind trait based on how Rc<T> acts as a container for T.
RcRefCellLike
A container kind trait based on how Rc<RefCell<T>> acts as a container for T.
TLike
A container kind trait based on how a type T acts as a container for itself.