Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a generic type that includes a trait

Tags:

rust

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.

like image 786
pm100 Avatar asked Aug 22 '20 01:08

pm100


People also ask

What are generic associated types?

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.

Where is trait Rust?

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.

What is trait object?

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.

What are associated types in Rust?

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.

How do you make a generic type in Java?

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.

What is makegenerictype in typescript?

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.

What is generics in C++?

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.

What is the purpose of generics?

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?


1 Answers

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>>;
  | 
like image 151
porton Avatar answered Oct 20 '22 20:10

porton