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

The trait std::convert::AsRef<[u8]> is not implemented for string.len() > 32 #17

Closed
ritiek opened this issue Nov 19, 2017 · 2 comments
Closed

Comments

@ritiek
Copy link

ritiek commented Nov 19, 2017

If we attempt to encode a string with string.len() greater than 32, we get an error.

Modified example

extern crate qrcode;
extern crate image;

use qrcode::QrCode;
use image::Luma;

fn main() {
    // Encode some data into bits.
    let code = QrCode::new(b"this string has length 50 which is greater than 32").unwrap();

    // Render the bits into an image.
    let image = code.render::<Luma<u8>>().build();

    // Save the image.
    image.save("/tmp/qrcode.png").unwrap();
}

Here is the error

error[E0277]: the trait bound `[u8; 50]: std::convert::AsRef<[u8]>` is not satisfied
 --> src/main.rs:9:16
  |
9 |     let code = QrCode::new(b"this string has length 50 which is greater than 32").unwrap();
  |                ^^^^^^^^^^^ the trait `std::convert::AsRef<[u8]>` is not implemented for `[u8; 50]`
  |
  = help: the following implementations were found:
            <[T; <unevaluated[]>] as std::convert::AsRef<[T]>>
            <[T; <unevaluated[]>] as std::convert::AsRef<[T]>>
            <[T; <unevaluated[]>] as std::convert::AsRef<[T]>>
            <[T; <unevaluated[]>] as std::convert::AsRef<[T]>>
          and 30 others
  = note: required because of the requirements on the impl of `std::convert::AsRef<[u8]>` for `&[u8; 50]`
  = note: required by `qrcode::QrCode::new`

As it seems from std::convert::AsRef, the trait AsRef is only implemented for arrays <= 32 elements but in this case the string gets converted to byte array which happens to be > 32 elements.

@kennytm
Copy link
Owner

kennytm commented Nov 19, 2017

This is Rust's limitation (rust-lang/rust#44580) which I can't solve in this crate alone 😄. You could turn the &[u8; N] into &[u8] through this though:

    let code = QrCode::new(&b"this string has length 50 which is greater than 32"[..]).unwrap();
    //                     ^                                                     ^^^^

@kennytm kennytm closed this as completed Nov 19, 2017
@ritiek
Copy link
Author

ritiek commented Nov 19, 2017

Thanks! That seems to work.

@kennytm kennytm mentioned this issue Sep 28, 2019
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

2 participants