Is it possible to create an instance of a generic parameter type in Rust?
I'm coming from a C++ background where it's perfectly valid to use template types to create types in the actual function body.
I'm trying to create a variable with type T in this function but I'm not sure how.
I just want to be able to create an object of type T, load it, and then insert it into the HashMap:
fn load<T>(&mut self, id: String, path: String)
where T: AssetTrait + 'static
{
// let asset : T = T; this doesn't work?
asset.load(path);
// self.assets.insert(id, Box::new<T>(asset));
}
Here is all my code:
trait AssetTrait {
fn load(&self, path: String) {
// Do nothing
// Implement this in child asset object
}
}
struct AssetManager {
assets: HashMap<String, Box<AssetTrait + 'static>>,
}
impl AssetManager {
fn new() -> AssetManager {
let asset_manager = AssetManager { assets: HashMap::new() };
return asset_manager;
}
fn load<T>(&mut self, id: String, path: String)
where T: AssetTrait + 'static
{
// let asset : T = T; this doesn't work?
asset.load(path);
// self.assets.insert(id, Box::new<T>(asset));
}
}