在Rust Generic性状实现中,如何避免“ T1”类型“ T1”是不受限制的。当我只需要T1的TypeID时?
我正在学习生锈并通过 typeid
进行实验。我正在寻找是否有可能在依赖性注入中制作一个简单的IOC容器,但是我遇到了一个函数的通用性状实现问题:
use std::any::{Any, TypeId};
trait IocInjectable<T> {
fn get_injectables_list(&self) -> Vec<TypeId>;
fn inject(&mut self, injectables: Vec<&dyn Any>) -> T;
}
impl<Func, T1, T2, TRet> IocInjectable<TRet> for Func
where
Func: Fn(&T1, &T2) -> TRet + 'static,
T1: 'static, // not sure, what goes here?
T2: 'static
{
fn get_injectables_list(&self) -> Vec<TypeId> {
return vec![TypeId::of::<T1>(), TypeId::of::<T2>()];
}
fn inject(&mut self, injectables: Vec<&dyn Any>) -> TRet {
let arg1 = injectables[0].downcast_ref::<T1>().unwrap();
let arg2 = injectables[1].downcast_ref::<T2>().unwrap();
return self(arg1, arg2);
}
}
我遇到了一个错误:
error[E0207]: the type parameter `T1` is not constrained by the impl trait, self type, or predicates
--> src/lib.rs:9:12
|
9 | impl<Func, T1, T2, TRet> IocInjectable<TRet> for Func
| ^^ unconstrained type parameter
”一般的功能?
更新在查看链接问题的答案后,我能够解决编译器错误。请注意,这不是最佳/最终/良好解决方案,这只是解决编译器错误。
use std::any::{Any, TypeId};
trait IocInjectable<TArgs> {
type TRet;
fn get_injectables_list(&self) -> Vec<TypeId>;
fn inject(&mut self, injectables: Vec<&dyn Any>) -> Self::TRet;
}
impl<Func, T1, T2, TRet1> IocInjectable<(T1, T2)> for Func
where
Func: Fn(&T1, &T2) -> TRet1 + 'static,
T1: 'static,
T2: 'static
{
type TRet = TRet1;
fn get_injectables_list(&self) -> Vec<TypeId> {
return vec![TypeId::of::<T1>(), TypeId::of::<T2>()];
}
fn inject(&mut self, injectables: Vec<&dyn Any>) -> TRet1 {
let arg1 = injectables[0].downcast_ref::<T1>().unwrap();
let arg2 = injectables[1].downcast_ref::<T2>().unwrap();
return self(arg1, arg2);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论