Skip to content

Cannot resolve size of either Self or generic type in method signature #48505

@npmccallum

Description

@npmccallum

It seems like this should work:

trait IntoBytes: Sized + Copy {
    fn into_bytes(&self, bytes: &mut [u8; size_of::<Self>()]) {
        let src = self as *const Self as *const u8;
        let dst: *mut u8 = &mut bytes[0];

        unsafe { std::ptr::copy_nonoverlapping::<u8>(src, dst, bytes.len()); }
    }
}

impl IntoBytes for u64 {}
impl IntoBytes for i64 {}

Or at least this:

trait IntoBytes<T: Sized>: Sized + Copy {
    fn into_bytes(&self, bytes: &mut [u8; size_of::<T>()]) {
        let src = self as *const Self as *const u8;
        let dst: *mut u8 = &mut bytes[0];

        unsafe { std::ptr::copy_nonoverlapping::<u8>(src, dst, bytes.len()); }
    }
}

impl IntoBytes<u8> for u64 {}
impl IntoBytes<u8> for i64 {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions