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


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

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?


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


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…


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


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


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.


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