generics - 特质不能变成对象
问题描述
我正在研究光线追踪器,并希望对所有可命中对象进行建模以提供通用接口。
我实现了一个名为 Object 的特征,所有可命中对象都实现了该特征。我创建了一个名为 Intersection 的结构,其中包含一个 f32 值和一个对实现 Object trait 的结构的引用。
编码:
use std::sync::atomic::{AtomicUsize, Ordering};
use super::ray::Ray;
use std::ops::{Index};
static mut ID : AtomicUsize = AtomicUsize::new(0);
pub trait Object {
fn intersection<'a, T: Object>(&self, ray: &Ray) -> Intersections<'a, T>;
fn get_uid() -> usize {
unsafe {
ID.fetch_add(1, Ordering::SeqCst);
ID.load(Ordering::SeqCst)
}
}
}
pub struct Intersection<'a, T: Object>{
pub t: f32,
pub obj: &'a T,
}
impl<'a, T: Object> Intersection<'a, T> {
pub fn new(t: f32, obj: &'a Object) -> Intersection<'a, T> {
Self {t, obj}
}
}
pub struct Intersections<'a, T: Object> {
pub hits: Vec<Intersection<'a, T>>,
}
impl<'a, T: Object> Intersections<'a, T> {
pub fn new() -> Self {
Self {
hits: Vec::new(),
}
}
pub fn push(&self, hit: Intersection<'a, T>) {
self.hits.push(hit);
}
pub fn len(&self) -> usize {
self.hits.len()
}
}
错误信息如下:
error[E0038]: the trait `object::Object` cannot be made into an object
--> src/object.rs:23:5
|
23 | pub fn new(t: f32, obj: &'a Object) -> Intersection<'a, T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `object::Object` cannot be made into an object
|
= note: method `intersection` has generic type parameters
= note: method `get_uid` has no receiver
由于我在 Intersection 中存储了一个引用,我认为它不必处理结构的实际大小。
解决方案
我很确定这Intersection
不应该是通用的,而是应该包含一个&Object
:
pub struct Intersection<'a>{
pub t: f32,
pub obj: &'a Object,
}
如果你真的需要实际的对象类型Intersection
,那么Object::intersection
不应该是通用的,而应该返回一个Intersection<Self>
:
pub trait Object<'a> {
fn intersection(&self, ray: &Ray) -> Intersections<'a, Self>;
}
错误的第二部分涉及get_uid
. 如果您想通过引用访问特征,它不能成为特征的一部分,因为self
在这种情况下只能使用带参数的函数。
另请注意,这get_uid
与您的想法不同:如果两个线程同时调用它,则两者都有可能获得相同的结果。你想要的是:
fn get_object_uid() -> usize { // <- Renamed because it needs to be outside the trait
unsafe {
ID.fetch_add (1, Ordering::SeqCst) + 1
}
}
推荐阅读
- reactjs - 用 Ant InputNumber 类型错误反应 Formik
- javascript - Kubernetes 容器,“取消”云存储通知
- cypress - 链接单个页面测试
- xcode - Xcode:如何使用 CMake 嵌入带有“复制代码签名”的框架?
- c# - TU 在 C# 泛型中代表什么?
- c# - C# 在脚本的另一部分中使用使用 Class 创建的变量
- sql-server - SQL 游标在同一会话中运行时返回 0 行
- angular - 使用环境时的 CORS 策略
- windows - 在 Windows 提示符下计时每个命令
- c# - 使用Entity Framework时,设置FK时应该设置导航属性还是外键属性?