#Enum based named constants (int / u8)

106 messages · Page 1 of 1 (latest)

limpid nexus
#

Hello, I am converting a diver for a i2c display, and to communicate it I need to use a series of set values.
For instance, in the Python driver, colours are defined as:

REG_RED    =     0x04
REG_GREEN  =     0x03
REG_BLUE   =     0x02
REG_MODE1  =     0x00
....

On the surface this seems like an ideal use case for enums to provide transparent human readable representations, but I don't know how to construct an enum with set conversions cleanly.
I was thinking of doing

impl From<color_enum> for u8 {
  fn from(color: color_enum) -> Self {
    match color {
      REG_RED => { 0x04 as u8}
......

But this seems very clunky, and that there should be a more easily readable way to achieve the same.

Priority is on the most clean looking final code in usage (for instance making u8 Vecs of these values).

Any and all help is appreciated, thanks!

#

Enum based named constants (int / u8)

queen notch
#

so for enum -> u8, you can just cast it using as if it doesn't have any data

#

for u8 -> enum, there's crates like num_derive that can do it automatically

limpid nexus
#

for the first one, how would i set that up to have those specific association values? I assume that would do random ordering from 0 or 1.

queen notch
#

you can tell it when you declare the enum

#

enum E { A = 5, ..

limpid nexus
#

Ah so if i just did (ignore minor errors if present)

enum color {
  RED = 0x04,
  GREEN = 0x03,
...
}
queen notch
#

yep

echo hemlock
#

You might need #[repr(u8)]?

#

Not sure about that one tho

queen notch
#

not necessarily

#

only if you want to pass pointers to the enum directly to c

echo hemlock
#

Also why is your color enum snake cased 👁️

limpid nexus
queen notch
#

python doesn't really enforce a style

echo hemlock
echo hemlock
#

I cant think of a language that doesnt do upper case type tho

queen notch
#

c

echo hemlock
#

Do they?

limpid nexus
#

Ty to you both! I've put in the repr just to be sure. Embedded rust sure is a... thing

echo hemlock
#

D:

echo hemlock
#

If it compiles without the repr tho tough done need it

queen notch
#

the whole std style for both c and cpp is snake or lowercase

echo hemlock
#

Could cost performance

queen notch
#

it doesn't

echo hemlock
echo hemlock
queen notch
#

rust will use the smallest type possible for the enum

limpid nexus
echo hemlock
#

(enum, value) could only niche when enum is not repr right?

queen notch
#

if it fits in one byte, it will use one byte

echo hemlock
echo hemlock
#

Were into gigahertz now, your phone runs at like 1.4Ghz

limpid nexus
#

idk dont question me im drowning enough ;-; and its an esp32

echo hemlock
#

So it is embedded?

limpid nexus
#

yeah, tho thankfully the manufacturer gives a abstraction layer with a alloc etc

queen notch
#

I can't say for an esp33, but on mainstream cpus, casting between types known to the cpu is free

limpid nexus
#

The compiler should inline it too

queen notch
#

but again, rust will make it an u8 if it can

#

eg. if all variants are under 256

echo hemlock
queen notch
#

the tuple is 2 bytes either way

echo hemlock
#

Why, can this not niche? Only enum with values?

queen notch
#

you're talking about using say, 2 bits per enum, correct?

echo hemlock
#

I mean, in this case it would be 1 bit per

queen notch
#

the problem is that the value must still be correct if you take a reference to it and give it to someone

#

which it won't be if you put more than one value in a byte

#

you can't take the reference to a bit

echo hemlock
#

Right since they are independent you can’t niche like this

queen notch
#

something like Option<Variant> works because you can never take the reference to the Variant when it's None

echo hemlock
#

But for something like Option<&T> yeah you said what I was gonna say

queen notch
#

and then it can just use 2 for None

#

Option<&T> is also a ref

#

so it will use null for None

echo hemlock
#

Ye

#

Null ptr optimization

#

Wonder if any platform is insane enough to have a none-zero value be null

queen notch
#

it's the same opts as for enums/bools/etc, it just finds a value that's not valid for the type

echo hemlock
#

Yeah that’s what I meant by niche

queen notch
#

also cpp has a type where nullptr is not 0 on mainstream platforms

#

because of course it does

echo hemlock
#

What why

queen notch
#

because cpp

echo hemlock
#

Of course

queen notch
#

of course

echo hemlock
#

Why?
CPP
Oh okay

#

Answer to the universe

queen notch
#

it's the pointer to a data member

echo hemlock
#

To a wot

queen notch
#

let me see if I can find the syntax, I can never remember it

echo hemlock
#

I’m sorry that you know cpp

queen notch
queen notch
#

that's a first

#

so this kind of pointer actually stores the offset in the struct of the field, and has to be used with a class instance

#

and 0 is a valid offset

#

so they use -1 for nullptr

echo hemlock
#

Why is an offset signed 😭

queen notch
#

it's not

#

int is the type of the data member

#

not of the offset

#

because int speed

echo hemlock
queen notch
#

ah

echo hemlock
#

It’s a semantic thing innit

queen notch
#

it's just a simple way to say 0xFFFF..

echo hemlock
#

Like all FF

#

I knew it

queen notch
#

easier to write

#

but it is unsigned technically

echo hemlock
#

2147 yeah I forgot

queen notch
#

it's FF all the way

#

now, we can get in a debate if an offset that is usize can actually be unsigned or not

#

because llvm for instance will treat all offsets with the same size as a pointer as signed

echo hemlock
#

D:

queen notch
#

because it's ub to have a memory area bigger than half the memory

echo hemlock
#

Wdym with the same size tho

#

With the same size as what

queen notch
#

as the pointer on that target platform

echo hemlock
#

Same size as _size? Ok

queen notch
#

yeah

#

I just realized you're not the OP of the thread lol