特质不能成为一个对象 [英] trait cannot be made into an object
问题描述
我正在使用光线跟踪器,并且希望对所有可击中对象进行建模以提供一个公共界面.
I'm working on a ray tracer and want to model all hitable objects to provide a common interface.
我实现了一个名为Object的特征,所有可点击对象都实现了该特征.我创建了一个名为Intersection的结构,其中包含f32值和对实现对象特征的结构的引用.
I implemented a trait named Object which all hitable objects implement. I created a struct called Intersection which contains an f32 value and a reference to struct that implements 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
由于我在交叉口中存储了引用,所以我认为它不必处理该结构的实际大小.
Since I'm storing a reference in the Intersection, I supposed it doesn't have to deal with the actual size of the struct.
推荐答案
我很确定 Intersection
不应是通用的,而应包含& Object 代码>:
I'm pretty sure that Intersection
shouldn't be generic, but should instead contain a &Object
:
pub struct Intersection<'a>{
pub t: f32,
pub obj: &'a Object,
}
如果您确实需要 Intersection
中的实际对象类型,则 Object :: intersection
不应是通用的,而应返回 Intersection< Self>
:
If you really need the actual object type in Intersection
, then Object::intersection
should not be generic, but should return an Intersection<Self>
:
pub trait Object<'a> {
fn intersection(&self, ray: &Ray) -> Intersections<'a, Self>;
}
错误的第二部分涉及 get_uid
.如果要通过引用访问特征,则不能将其作为特征的一部分,因为在这种情况下,只能使用带有 self
参数的函数.
The second part of the error concerns get_uid
. It can't be part of the trait if you want to access the trait through references because only functions that take a self
parameter can be used in this situation.
还请注意, get_uid
并没有您所想的那样:如果两个线程同时调用它,则有可能两个线程都将获得相同的结果.您想要的是:
Note also that get_uid
doesn't do what you think: if two threads call it simultaneously, there is a chance that both will get the same result. What you want is:
fn get_object_uid() -> usize { // <- Renamed because it needs to be outside the trait
unsafe {
ID.fetch_add (1, Ordering::SeqCst) + 1
}
}
这篇关于特质不能成为一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!