Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
npmccallum opened this issue Feb 24, 2018 · 3 comments
Closed

Comments

@npmccallum
Copy link
Contributor

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 {}
@npmccallum
Copy link
Contributor Author

This seems to be related to #43408, but I also raise the possibility of Self.

@hanna-kruppe
Copy link
Contributor

hanna-kruppe commented Feb 24, 2018

Self in traits is essentially a generic parameter so this is really the same issue.

@pietroalbini
Copy link
Member

Closing as a duplicate.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants