I find I am using this pattern a lot.
Arc<Mutex<dyn SomeTrait + Send>>;
and so I thought I would do this:
pub type NicePtr<T> = Arc<Mutex<dyn T + Send>>;
but this does not compile
Compiling rsim v0.1.0 (C:\work\pdp\rsim)
error[E0404]: expected trait, found type parameter `T`
--> src\common.rs:9:37
|
9 | pub type NicePtr<T> = Arc<Mutex<dyn T + Send>>;
| ^ not a trait
I assume this is possible, but I just dont know the correct syntax.
GATs (generic associated types) were originally proposed in RFC 1598. As said before, they allow you to define type, lifetime, or const generics on associated types. If you're familiar with languages that have "higher-kinded types", then you could call GATs type constructors on traits.
A trait in Rust is a group of methods that are defined for a particular type. Traits are an abstract definition of shared behavior amongst different types. So, in a way, traits are to Rust what interfaces are to Java or abstract classes are to C++. A trait method is able to access other methods within that trait.
A trait object is an opaque value of another type that implements a set of traits. The set of traits is made up of an object safe base trait plus any number of auto traits. Trait objects implement the base trait, its auto traits, and any supertraits of the base trait.
Associated Types in Rust are similar to Generic Types; however, Associated Types limit the types of things a user can do, which consequently facilitates code management. Among the Generic Types of traits, types that depend on the type of trait implementation can be expressed by using the Associated Type syntax.
To do this, call the MakeGenericType method on a Type object representing the class Base, using the generic type arguments Int32 and the type parameter V from Derived. Because types and generic type parameters are both represented by Type objects, an array containing both can be passed to the MakeGenericType method.
True */ The MakeGenericType method allows you to write code that assigns specific types to the type parameters of a generic type definition, thus creating a Type object that represents a particular constructed type. You can use this Type object to create run-time instances of the constructed type.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.
The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a parameterized type is a generic entity. Why Generics?
An interesting note is that (it is the thing that you want)
use std::sync::{Arc, Mutex};
pub type NicePtr<T: Send> = Arc<Mutex<T>>;
does compile, but with a warning:
warning: bounds on generic parameters are not enforced in type aliases
--> src/lib.rs:2:21
|
2 | pub type NicePtr<T: Send> = Arc<Mutex<T>>;
| ^^^^
|
= note: `#[warn(type_alias_bounds)]` on by default
help: the bound will not be checked when the type alias is used, and should be removed
|
2 - pub type NicePtr<T: Send> = Arc<Mutex<T>>;
2 + pub type NicePtr<T> = Arc<Mutex<T>>;
|
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With