CAN `StandardId` and `ExtendedId`: duplicating constant literals is not good practice
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 55/100
- Tipo di issue
- Refactoring
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Tranquilla
- Stack tecnologico
- rust
- Ambito
- embedded-iot
Direzione di ricerca
Locate the StandardId and ExtendedId definitions and inspect how their raw-ID bounds and bit operations are currently expressed. Refactor duplicated literals into shared or type-specific constants, preserve the existing behavior, and decide from the issue's API discussion whether the raw bound should be public; verify that the affected code still builds and its existing checks pass.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
This is how StandardId is currently implemented (ExtendedId is implemented the same way):
impl StandardId {
/// CAN ID `0`, the highest priority.
pub const ZERO: Self = Self(0);
/// CAN ID `0x7FF`, the lowest priority.
pub const MAX: Self = Self(0x7FF);
/// Tries to create a `StandardId` from a raw 16-bit integer.
///
/// This will return `None` if `raw` is out of range of an 11-bit integer (`> 0x7FF`).
#[inline]
#[must_use]
pub const fn new(raw: u16) -> Option<Self> {
if raw <= 0x7FF {
Some(Self(raw))
} else {
None
}
}
Duplicating constant literals (0x7FF) is error-prone and not good practice overall.
Instead, it should be implemented somewhat like this:
impl StandardId {
/// CAN Standard ID bit size
const BITS: u8 = 11;
/// Max 11-bit raw ID (`0x7FF`)
const MAX_RAW_ID: u16 = (1u16 << Self::BITS) - 1;
/// Min CAN ID (highest priority).
pub const ZERO: Self = Self(0);
/// Max CAN ID (lowest Standard priority).
pub const MAX: Self = Self(Self::MAX_RAW_ID);
/// Tries to create a `StandardId` from a raw 16-bit integer.
///
/// This will return `None` if `raw` is out of range of an 11-bit integer (`> MAX_RAW_ID`).
#[inline]
#[must_use]
pub const fn new(raw: u16) -> Option<Self> {
if raw <= Self::MAX_RAW_ID {
Some(Self(raw))
} else {
None
}
}
Note: There is no real reason to keep MAX_RAW_ID private, except that it might saw confusion in users by colliding with MAX, seemingly the way we intend them to compare StandardIds with each other (rather than by raw ID proxy).
Optionally, if we elect to decouple BITS and MAX_RAW_ID as above, we can also make other bit-mangling methods in ExtendedId look less arbitrary. For example (assuming #742):
/// Returns the Base ID part of this extended identifier.
#[inline(always)]
pub const fn base_part(&self) -> u16 {
// Extract the 11 most significant bits from this 29-bit identifier
(self.as_raw() >> (ExtendedId::BITS - StandardId::BITS)) as u16
}
/// Returns the Extended ID part of this extended identifier.
#[inline(always)]
pub const fn extended_part(&self) -> u32 {
// Extract the 18 least significant bits from this 29-bit identifier
self.as_raw() & ((1 << (ExtendedId::BITS - StandardId::BITS)) - 1)
}
- Lingua principale
- Rust
- Stelle
- 2.7k
- Fork
- 283
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di rust-embedded/embedded-hal
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
rust-embedded/embedded-hal#742 · 1 commento ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 30/100
rust-embedded/embedded-hal#747 · 5 commenti · 1 reazione ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
rust-embedded/embedded-hal#746 · 2 commenti · 1 reazione ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
rust-embedded/embedded-hal#745 · 1 reazione ·
-
CAN: returning `Result` instead of `Option`Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
rust-embedded/embedded-hal#741 · 2 reazioni ·
Tutte le issue di rust-embedded/embedded-hal
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
bytecodealliance/rustix#1703 ·
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
linux build warningAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
Pomax/vst-plugins#24 ·
I maintainer di solito rispondono entro 1 giorno
-
V1 manifest lists written by Java lose added/existing/deleted file countsForse già presa @kacper-li-airspace-intelligence l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
apache/iceberg-rust#3445 ·
I maintainer di solito rispondono entro 4 giorni
-
Slack integration: links on self-hosted domains never unfurl (parseCapShareUrl only accepts cap.so / cap.link)Forse già presa @mangeshraut712 l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
CapSoftware/Cap#2546 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno