Search code examples
genericsrusttraitstrait-objects

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.

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.

The code:

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()
    }
}

The error message is as follows:

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.


Solution

  • 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,
    }
    

    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>;
    }
    

    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.

    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
        }
    }