Why does get() work on Cell<&Trait>?


#1

Hi, new to Rust :slight_smile: Could anybody please explain why does this work?

use std::cell::Cell;

trait Trait{}
impl Trait for i32{}

fn main() {
    let i = 4;
    let c : Cell<&Trait> = Cell::new(&i);
    c.get();
}

Sources say

impl<T:Copy> Cell<T> {
    ...
    pub fn get(&self) -> T {
        unsafe{ *self.value.get() }
    }

so why can I use get even though &Trait does not extend Copy?


#2

&Trait does implement Copy. All &T references do.


#3

Thx a lot Rk, that would explain it.
I’m still confused though because Copy doc doesn’t say so…
So this

impl <T : ?Sized> Copy for &T{}

must sit elsewhere? Is there generally a good way to check if a type implements a Trait? And why? :slight_smile:
I’m missing my type hierarchy feature in Eclipse which allows me to explore JDK…


#4

Rustdoc is unfortunately buggy and doesn’t always list all trait implementations. It lists those which are written out in code somewhere, but it misses those which are defined by the compiler. Note that that page also misses i32 and other types which are Copy.

You can test it experimentally by writing code like this and seeing if it throws an error:

fn assert_copy<T: Copy>() {}
assert_copy::<i32>();

#5

Do you (or anyone else) happen to know where in rustc these builtin Copy types are listed/defined? Just curious.


#6

I don’t know where primitives actually get their Copy, but you might like this post about lang items, including how Copy itself is defined.


#7

Thanks - an article about internals with code links? Sign me up! :slight_smile: