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

Nightly no u8 -> u16 conversion #46

Open
mobergmann opened this issue Jul 24, 2024 · 1 comment
Open

Nightly no u8 -> u16 conversion #46

mobergmann opened this issue Jul 24, 2024 · 1 comment

Comments

@mobergmann
Copy link

I am compiling an embedded program for the Arduino, requiring the rust nightly version. It seems, that this crate cannot convert u8 to u16 on the nightly version.

[nix-shell:~/Source/Bachelorarbeit/Code/experiments]$ cargo --version
cargo 1.79.0-nightly (d438c80c4 2024-03-19)

[nix-shell:~/Source/Bachelorarbeit/Code/experiments]$ rustc --version
rustc 1.79.0-nightly (0ad927c0c 2024-03-21)

My code, basically the example from the readme with all necessary steps for the embedded code.

#![no_std]
#![no_main]

extern crate cast;

use panic_halt as _;

// `u8` and `u16` are checked cast functions, use them to cast from any numeric
// primitive to `u8`/`u16` respectively
use cast::u16;

#[arduino_hal::entry]
fn main() -> ! {    
    loop {
        // cast with `as`
        assert_eq!(u16(0u8), 0u16);
    }
}

Compilation error:

   Compiling experiments v0.1.0 (/home/mobergmann/Source/Bachelorarbeit/Code/experiments)
error[E0277]: the trait bound `u16: cast::From<u8>` is not satisfied
   --> src/main.rs:16:24
    |
16  |         assert_eq!(u16(0u8), 0u16);
    |                    --- ^^^ the trait `cast::From<u8>` is not implemented for `u16`
    |                    |
    |                    required by a bound introduced by this call
    |
    = help: the following other types implement trait `cast::From<Src>`:
              <u16 as cast::From<i128>>
              <u16 as cast::From<u128>>
note: required by a bound in `u16`
   --> /home/mobergmann/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cast-0.3.0/src/lib.rs:172:1
    |
172 | fns!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---^^^^^^^^^^^^^^^^^^
    | |                                            |
    | |                                            required by a bound in this function
    | required by this bound in `u16`
    = note: this error originates in the macro `fns` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: cast::From<u8>` is not satisfied
  --> src/main.rs:16:20
   |
16 |         assert_eq!(u16(0u8), 0u16);
   |                    ^^^^^^^^ the trait `cast::From<u8>` is not implemented for `u16`
   |
   = help: the following other types implement trait `cast::From<Src>`:
             <u16 as cast::From<i128>>
             <u16 as cast::From<u128>>

For more information about this error, try `rustc --explain E0277`.
error: could not compile `experiments` (bin "experiments") due to 2 previous errors

I believe the error is appropriate in this repo. If i am wrong, please inform me.

@d2weber
Copy link

d2weber commented Sep 10, 2024

I believe this is not a matter of compiling with nightly, rather that you're compiling for a target with "target-pointer-width": "16". At least that was the reason why I got the error.

The reason is that the necessary traits are only implemented for 32 bit and 64 bit, see here

cast.rs/src/lib.rs

Lines 354 to 468 in 0522880

#[cfg(target_pointer_width = "32")]
mod _32 {
use crate::{Error, From};
// Signed
promotion! {
i8 => f32, f64, i8, i16, i32, isize, i64;
i16 => f32, f64, i16, i32, isize, i64;
i32 => f32, f64, i32, isize, i64;
isize => f32, f64, i32, isize, i64;
i64 => f32, f64, i64;
}
half_promotion! {
i8 => u8, u16, u32, usize, u64;
i16 => u16, u32, usize, u64;
i32 => u32, usize, u64;
isize => u32, usize, u64;
i64 => u64;
}
from_signed! {
i16 => i8, u8;
i32 => i8, i16, u8, u16;
isize => i8, i16, u8, u16;
i64 => i8, i16, i32, isize, u8, u16, u32, usize;
}
// Unsigned
promotion! {
u8 => f32, f64, i16, i32, isize, i64, u8, u16, u32, usize, u64;
u16 => f32, f64, i32, isize, i64, u16, u32, usize, u64;
u32 => f32, f64, i64, u32, usize, u64;
usize => f32, f64, i64, u32, usize, u64;
u64 => f32, f64, u64;
}
from_unsigned! {
u8 => i8;
u16 => i8, i16, u8;
u32 => i8, i16, i32, isize, u8, u16;
usize => i8, i16, i32, isize, u8, u16;
u64 => i8, i16, i32, isize, i64, u8, u16, u32, usize;
}
// Float
promotion! {
f32 => f32, f64;
f64 => f64;
}
from_float! {
f32 => i8, i16, i32, isize, i64, u8, u16, u32, usize, u64;
f64 => i8, i16, i32, isize, i64, u8, u16, u32, usize, u64;
}
}
#[cfg(target_pointer_width = "64")]
mod _64 {
use crate::{Error, From};
// Signed
promotion! {
i8 => f32, f64, i8, i16, i32, i64, isize;
i16 => f32, f64, i16, i32, i64, isize;
i32 => f32, f64, i32, i64, isize;
i64 => f32, f64, i64, isize;
isize => f32, f64, i64, isize;
}
half_promotion! {
i8 => u8, u16, u32, u64, usize;
i16 => u16, u32, u64, usize;
i32 => u32, u64, usize;
i64 => u64, usize;
isize => u64, usize;
}
from_signed! {
i16 => i8, u8;
i32 => i8, i16, u8, u16;
i64 => i8, i16, i32, u8, u16, u32;
isize => i8, i16, i32, u8, u16, u32;
}
// Unsigned
promotion! {
u8 => f32, f64, i16, i32, i64, isize, u8, u16, u32, u64, usize;
u16 => f32, f64, i32, i64, isize, u16, u32, u64, usize;
u32 => f32, f64, i64, isize, u32, u64, usize;
u64 => f32, f64, u64, usize;
usize => f32, f64, u64, usize;
}
from_unsigned! {
u8 => i8;
u16 => i8, i16, u8;
u32 => i8, i16, i32, u8, u16;
u64 => i8, i16, i32, i64, isize, u8, u16, u32;
usize => i8, i16, i32, i64, isize, u8, u16, u32;
}
// Float
promotion! {
f32 => f32, f64;
f64 => f64;
}
from_float! {
f32 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
f64 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
}
}

You might want to implement the 16 bit equivalent. I just opted not using this crate but plain old conversions usize::from(..) or u16::try_from(..).unwrap()

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