Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

CAN `StandardId` and `ExtendedId`: duplicating constant literals is not good practice

Aperta
#744 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-embedded/embedded-hal

Tutte le issue di rust-embedded/embedded-hal

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.