Skip to content

Impling a trait on T fails to make it accessable for generic types. #6898

Closed
@brendanzab

Description

@brendanzab

Consider the following code:

use std::unstable::intrinsics;

/// Returns the size of a type
pub fn size_of<T>() -> uint {
    TypeInfo::size_of::<T>()
}

/// Returns the size of the type that `val` points to
pub fn size_of_val<T>(val: &T) -> uint {
    val.size_of_val()
}

pub trait TypeInfo {
    pub fn size_of() -> uint;
    pub fn size_of_val(&self) -> uint;
}

impl<T> TypeInfo for T {
    /// The size of the type in bytes.
    pub fn size_of() -> uint {
        unsafe { intrinsics::size_of::<T>() }
    }

    /// Returns the size of the type of `self` in bytes.
    pub fn size_of_val(&self) -> uint {
        TypeInfo::size_of::<T>()
    }
}

fn main() {}

I get the error:

type-info.rs:5:4: 5:26 error: failed to find an implementation of trait TypeInfo for 'a
type-info.rs:5     TypeInfo::size_of::<T>()
                   ^~~~~~~~~~~~~~~~~~~~~~

Commenting out the size_of function, I then get this:

type-info.rs:26:8: 26:30 error: failed to find an implementation of trait TypeInfo for 'a
type-info.rs:26         TypeInfo::size_of::<T>()
                        ^~~~~~~~~~~~~~~~~~~~~~

I would have assumed that seeing as type T affects all types you would be able to use the impled methods even if there was no type constraint specified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions