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 forT. - ArcMutex
Kind - The container kind corresponding to
Arc<Mutex<T>>as a container forT. - ArcRw
Lock Kind - The container kind corresponding to
Arc<RwLock<T>>as a container forT. - ArcThread
Checked Mutex Kind - The container kind corresponding to
Arc<ThreadCheckedMutex<T>>as a container forT. - BoxKind
- The container kind corresponding to
Box<T>as a container forT. - Checked
RcRef Cell Kind - The container kind corresponding to
CheckedRcRefCell<T>as a container forT. - RcKind
- The container kind corresponding to
Rc<T>as a container forT. - RcRef
Cell Kind - The container kind corresponding to
Rc<RefCell<T>>as a container forT. - TKind
- The container kind corresponding to
Tas a container for itself.
Traits§
- ArcLike
- A container kind trait based on how
Arc<T>acts as a container forT. - ArcMutex
Like - A container kind trait based on how
Arc<Mutex<T>>acts as a container forT. - ArcRw
Lock Like - A container kind trait based on how
Arc<RwLock<T>>acts as a container forT. - ArcThread
Checked Mutex Like - A container kind trait based on how
Arc<ThreadCheckedMutex<T>>acts as a container forT. - BoxLike
- A container kind trait based on how
Box<T>acts as a container forT. - Checked
RcRef Cell Like - A container kind trait based on how
CheckedRcRefCell<T>acts as a container forT. - Fragile
ArcLike - A container kind trait based on how
Arc<T>acts as a container forT. - Fragile
BoxLike - A container kind trait based on how
Box<T>acts as a container forT. - Fragile
RcLike - A container kind trait based on how
Rc<T>acts as a container forT. - FragileT
Like - A container kind trait based on how a type
Tacts as a container for itself. - RcLike
- A container kind trait based on how
Rc<T>acts as a container forT. - RcRef
Cell Like - A container kind trait based on how
Rc<RefCell<T>>acts as a container forT. - TLike
- A container kind trait based on how a type
Tacts as a container for itself.