Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Mismatched types on trait implementation with const generics #273

Open
Rexagon opened this issue Dec 22, 2021 · 0 comments
Open

Mismatched types on trait implementation with const generics #273

Rexagon opened this issue Dec 22, 2021 · 0 comments

Comments

@Rexagon
Copy link

Rexagon commented Dec 22, 2021

The following code gives a compilation error despite the fact that parameter A satisfies the required traits (Array, and A::Item: Copy).

smallvec = { version = "1.7", features = ["const_generics"] }

use smallvec::{Array, SmallVec};

trait StrangeWrite {
    fn expand(&mut self, bytes: &[u8]);
}

impl<const N: usize> StrangeWrite for SmallVec<[u8; N]>
where
    [u8; N]: Array,
    <[u8; N] as Array>::Item: Copy,
{
    fn expand(&mut self, bytes: &[u8]) {
        self.extend_from_slice(bytes);
    }
}
error[E0308]: mismatched types
   --> src/traits.rs:115:32
    |
115 |         self.extend_from_slice(bytes);
    |                                ^^^^^ expected associated type, found `u8`
    |
    = note: expected reference `&[<[u8; N] as smallvec::Array>::Item]`
               found reference `&[u8]`
    = help: consider constraining the associated type `<[u8; N] as smallvec::Array>::Item` to `u8` or calling a method that returns `<[u8; N] as smallvec::Array>::Item`
    = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

Am I doing something wrong or are there any problems with const generics?

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

No branches or pull requests

1 participant