One-shot APIs -- static functions vs methods

Aperta
#67 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
45/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
rust
Ambito
api

Direzione di ricerca

Start by inspecting the Hash trait and HashFactory, then search the repository for other traits using the ::new().hash(..) pattern. Determine whether the factory can support static ::hash(..) calls while remaining dyn-compatible; if not, apply the proposed dual-method approach. Done means similar patterns are cleaned up and crate documentation uses the static form.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Consider:

let output: Vec<u8> = sha3::SHA3_256::new().hash(data);

The pattern of ::new().hash(..), where .hash(..) is a method, is a code smell since should really be a static function ::hash(..). The reason why it's like that is because the HashFactory requires the Hash trait to be dyn-compatible, and I was not able to get the factory to work with a static ::hash(..).

The task for this ticket is to play some more with whether it's possible to get the HashFactory to chain properly to a ::hash(..). If not, @npajkovsky suggested that we could cheat and simply have the Hash trait have both versions:

trait Hash {
  fn hash(data: &[u8]) -> Vec<u8> {
    Self::new().hash_method(data)
  };

  fn hash_method(self, data: &[u8]) -> Vec<u8>;
}

and then we continue to use the .hash_method(data) version within the HashFactory, but we can simplify the sample code in the crate docs to use the cleaner SHA256::hash(data) version.

Note: I'm using Hash as an example, but this smelly pattern exists across other traits as well. This ticket should clean up all similar patterns.

Lingua principale
Rust
Stelle
25
Fork
18
Merge medio
16h 38m
PR unite (30g)
3

Guida per i contributori

Apri la guida per i contributori

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 bcgit/bc-rust

Tutte le issue di bcgit/bc-rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.