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

Doesn't work with repr(C) #10

Closed
elmarco opened this issue Sep 3, 2020 · 1 comment · Fixed by #24
Closed

Doesn't work with repr(C) #10

elmarco opened this issue Sep 3, 2020 · 1 comment · Fixed by #24

Comments

@elmarco
Copy link

elmarco commented Sep 3, 2020

Hi

An enum with a repr(C) will fail to compile:

diff --git a/tests/test.rs b/tests/test.rs
index e7e9eff..34ea048 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -4,7 +4,7 @@ mod small_prime {
     use super::*;
 
     #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
-    #[repr(u8)]
+    #[repr(C)]
     enum SmallPrime {
         Two = 2,
         Three = 3,
   Compiling serde_repr v0.1.6 (/home/elmarco/src/serde-repr)
error[E0412]: cannot find type `C` in this scope
 --> tests/test.rs:7:12
  |
6 |     #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
  |              -------------- similarly named type parameter `S` defined here
7 |     #[repr(C)]
  |            ^ help: a type parameter with a similar name exists: `S`

error[E0412]: cannot find type `C` in this scope
 --> tests/test.rs:7:12
  |
6 |     #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
  |                              ---------------- similarly named type parameter `D` defined here
7 |     #[repr(C)]
  |            ^ help: a type parameter with a similar name exists: `D`

Is there a workaround or possible solution ? (is it possible to get the underlying chosen int?)

@dtolnay
Copy link
Owner

dtolnay commented Oct 30, 2023

This is not supported by this crate. The repr is required to be a specific integer type.

But cannot find type `C` in this scope is not a good error message. I'll make a PR to change that.

To support repr(C), you could look for, or create, an attribute macro that inspects a set of enum discriminants and expands to the same integer repr that would be chosen by a C compiler.

#[infer_C_repr]
#[derive(Serialize_repr)]
pub enum Enum {
    ...
}

// expands to:
#[repr(i16)] // for example
#[derive(Serialize_repr)]
pub enum Enum {
    ...
}

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

Successfully merging a pull request may close this issue.

2 participants