diff --git a/src/agents/all.txt b/src/agents/all.txt index 112f61b..af8c621 100644 --- a/src/agents/all.txt +++ b/src/agents/all.txt @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5cf536f19c53ecd003897c2065c044b1abaf17612e0838f69a7fe0ce547ad650 -size 136640 +oid sha256:4267cb3a998e310395160fe4ab1b31afdcfdf3fa822dcfb1c379ada57dacc7ef +size 136853 diff --git a/src/guidelines/checklist/README.md b/src/guidelines/checklist/README.md index 46a3889..dd7341e 100644 --- a/src/guidelines/checklist/README.md +++ b/src/guidelines/checklist/README.md @@ -151,7 +151,7 @@ [M-INTEGRATION-TESTS]: ../libs/resilience/#M-INTEGRATION-TESTS [M-AVOID-STATICS]: ../libs/resilience/#M-AVOID-STATICS [M-OOBE]: ../libs/building/#M-OOBE -[M-SYS-CRATES]: ../libs/resilience/#M-SYS-CRATES +[M-SYS-CRATES]: ../libs/building/#M-SYS-CRATES [M-FEATURES-ADDITIVE]: ../libs/building/#M-FEATURES-ADDITIVE diff --git a/src/guidelines/correctness/M-PANIC-MESSAGE.md b/src/guidelines/correctness/M-PANIC-MESSAGE.md index 792d0d2..ea15431 100644 --- a/src/guidelines/correctness/M-PANIC-MESSAGE.md +++ b/src/guidelines/correctness/M-PANIC-MESSAGE.md @@ -6,6 +6,8 @@ When code panics intentionally (via `panic!`, `assert!`, `unreachable!`, `todo!`, or similar), a message must be present to clearly state what went wrong and, where applicable, include relevant values. +Use `expect("reason")` instead of `unwrap()` so the panic message explains why an `Option` should contain a value or a `Result` should succeed. + ```rust,ignore // Bad, the panic gives the developer little to act on. assert!(buffer.len() >= HEADER_SIZE); diff --git a/src/guidelines/correctness/M-PANIC-ON-BUG.md b/src/guidelines/correctness/M-PANIC-ON-BUG.md index 20a1176..62068fd 100644 --- a/src/guidelines/correctness/M-PANIC-ON-BUG.md +++ b/src/guidelines/correctness/M-PANIC-ON-BUG.md @@ -30,7 +30,7 @@ fn divide_by_fast(x: u32, y: u32) -> u32 { ... } // Here, passing an invalid URI is not a contract violation. // Since parsing is inherently fallible, a Result must be returned. -fn parse_uri(s: &str) -> Result { }; +fn parse_uri(s: &str) -> Result { ... } ``` diff --git a/src/guidelines/docs/M-MODULE-DOCS.md b/src/guidelines/docs/M-MODULE-DOCS.md index 25d7c45..317a47d 100644 --- a/src/guidelines/docs/M-MODULE-DOCS.md +++ b/src/guidelines/docs/M-MODULE-DOCS.md @@ -4,13 +4,13 @@ easy API docs navigation. -Any public library module must have `//!` module documentation, and the first sentence must follow [M-DOC-FIRST-SENTENCE]. +Any public library module must have `//!` module documentation, and the first sentence must follow [M-FIRST-DOC-SENTENCE]. ```rust,edition2021,ignore pub mod ffi { //! Contains FFI abstractions. - pub struct String {}; + pub struct String {} } ``` @@ -32,4 +32,4 @@ The rest of the module documentation should be comprehensive, i.e., cover the mo This does not mean every module should contain all of these items. But if there is something to say about the interaction of the contained types, their module documentation is the right place. -[M-DOC-FIRST-SENTENCE]: ./#M-DOC-FIRST-SENTENCE +[M-FIRST-DOC-SENTENCE]: ./#M-FIRST-DOC-SENTENCE diff --git a/src/guidelines/ffi/M-FFI-TRANSLATES.md b/src/guidelines/ffi/M-FFI-TRANSLATES.md index 1020aea..caf254e 100644 --- a/src/guidelines/ffi/M-FFI-TRANSLATES.md +++ b/src/guidelines/ffi/M-FFI-TRANSLATES.md @@ -23,14 +23,14 @@ impl Message { A proper separation of concerns might collapse construction and transmission into a single FFI entry point in `foo-ffi`: ```rust,ignore -#[no_mangle] +#[unsafe(no_mangle)] pub unsafe extern "C" fn transmit_message( destination: *const [u8; 8], data: *const u8, data_len: usize, ) -> u8 { - let data = std::slice::from_raw_parts(data, data_len).to_vec(); - match Message::new(*destination, data).transmit() { + let data = unsafe { std::slice::from_raw_parts(data, data_len) }.to_vec(); + match Message::new(unsafe { *destination }, data).transmit() { Ok(()) => 0, Err(_) => 1, } diff --git a/src/guidelines/libs/interop/M-IMPL-IO.md b/src/guidelines/libs/interop/M-IMPL-IO.md index 1d6b9df..dbfb6e5 100644 --- a/src/guidelines/libs/interop/M-IMPL-IO.md +++ b/src/guidelines/libs/interop/M-IMPL-IO.md @@ -27,6 +27,7 @@ fn parse_data(data: impl std::io::Read) {} Synchronous functions should use [`std::io::Read`](https://doc.rust-lang.org/std/io/trait.Read.html) and [`std::io::Write`](https://doc.rust-lang.org/std/io/trait.Write.html). Asynchronous _functions_ targeting more than one runtime should use [`futures::io::AsyncRead`](https://docs.rs/futures/latest/futures/io/trait.AsyncRead.html) and similar. -_Types_ that need to perform runtime-specific, continuous I/O should follow [M-RUNTIME-ABSTRACTED] instead. +_Types_ that need to perform runtime-specific, continuous I/O should follow the runtime-dispatch enum pattern illustrated in +[M-MOCKABLE-SYSCALLS] instead. -[M-RUNTIME-ABSTRACTED]: ./#M-RUNTIME-ABSTRACTED +[M-MOCKABLE-SYSCALLS]: ../resilience/#M-MOCKABLE-SYSCALLS diff --git a/src/guidelines/libs/resilience/M-MOCKABLE-SYSCALLS.md b/src/guidelines/libs/resilience/M-MOCKABLE-SYSCALLS.md index 7d4da94..27ba439 100644 --- a/src/guidelines/libs/resilience/M-MOCKABLE-SYSCALLS.md +++ b/src/guidelines/libs/resilience/M-MOCKABLE-SYSCALLS.md @@ -50,7 +50,7 @@ impl Library { } ``` -Behind the scenes, `LibraryCore` is a non-public enum, similar to [M-RUNTIME-ABSTRACTED], that either dispatches +Behind the scenes, `LibraryCore` is a non-public enum that either dispatches calls to the respective sys call, or to an mocking controller. ```rust, ignore @@ -65,10 +65,11 @@ enum LibraryCore { impl LibraryCore { // Some function you'd forward to the operating system. - fn random_u32(&self) { + fn random_u32(&self) -> u32 { match self { - Self::Native => unsafe { os_random_u32() } - Self::Mocked(m) => m.random_u32() + Self::Native => unsafe { os_random_u32() }, + #[cfg(feature = "test-util")] + Self::Mocked(m) => m.random_u32(), } } } @@ -86,7 +87,7 @@ mod mock { // `MockCtrlInner` below. impl MockCtrl { pub fn set_next_u32(&self, x: u32) { ... } - pub fn random_u32(&self) { ... } + pub fn random_u32(&self) -> u32 { ... } } // Contains actual logic, e.g., the next random number we should return. @@ -96,7 +97,7 @@ mod mock { } ``` -Runtime-aware libraries already build on top of the [M-RUNTIME-ABSTRACTED] pattern should extend their runtime enum instead: +Runtime-aware libraries that already dispatch through a runtime enum should extend that enum instead: ```rust, ignore enum Runtime { @@ -104,7 +105,7 @@ enum Runtime { Tokio(tokio::Tokio), #[cfg(feature="smol")] - Smol(smol::Smol) + Smol(smol::Smol), #[cfg(feature="test-util")] Mock(mock::MockCtrl) @@ -118,8 +119,6 @@ instances shared a single controller: ```rust, ignore impl Library { pub fn new_mocked() -> (Self, MockCtrl) { ... } // good - pub fn new_mocked_bad(&mut MockCtrl) -> Self { ... } // prone to misuse + pub fn new_mocked_bad(mock: &mut MockCtrl) -> Self { ... } // prone to misuse } ``` - -[M-RUNTIME-ABSTRACTED]: ../ux/#M-RUNTIME-ABSTRACTED diff --git a/src/guidelines/libs/ux/M-AVOID-WRAPPERS.md b/src/guidelines/libs/ux/M-AVOID-WRAPPERS.md index 7fb4132..8b0ed77 100644 --- a/src/guidelines/libs/ux/M-AVOID-WRAPPERS.md +++ b/src/guidelines/libs/ux/M-AVOID-WRAPPERS.md @@ -4,7 +4,7 @@ low cognitive load and ergonomic APIs. -As a specialization of [M-ABSTRACTIONS-DONT-NEST], generic wrappers and smart pointers like +As a specialization of [M-SIMPLE-ABSTRACTIONS], generic wrappers and smart pointers like `Rc`, `Arc`, `Box`, or `RefCell` should be avoided in public APIs. From a user perspective these are mostly implementation details, and introduce infectious complexity that users have to @@ -28,4 +28,4 @@ Smart pointers in APIs are acceptable when: - The smart pointer, based on benchmarks, significantly improves performance and the complexity is justified. -[M-ABSTRACTIONS-DONT-NEST]: ./#M-ABSTRACTIONS-DONT-NEST +[M-SIMPLE-ABSTRACTIONS]: ./#M-SIMPLE-ABSTRACTIONS diff --git a/src/guidelines/libs/ux/M-DI-HIERARCHY.md b/src/guidelines/libs/ux/M-DI-HIERARCHY.md index baa450b..38582d0 100644 --- a/src/guidelines/libs/ux/M-DI-HIERARCHY.md +++ b/src/guidelines/libs/ux/M-DI-HIERARCHY.md @@ -35,7 +35,7 @@ _on top_ of your inherent functions. Each trait should be relatively narrow, e.g trait is needed it should be made a subtrait, e.g., `trait DataAccess: StoreObject + LoadObject {}`. Code working with these traits should ideally accept them as generic type parameters as long as their use does not contribute to significant nesting -(compare [M-ABSTRACTIONS-DONT-NEST]). +(compare [M-SIMPLE-ABSTRACTIONS]). ```rust,ignore // Good, generic does not have infectious impact, uses only most specific trait @@ -84,4 +84,4 @@ async fn read_database(x: &DataAccess) { ... } [M-AVOID-WRAPPERS]: ./#M-AVOID-WRAPPERS [M-MOCKABLE-SYSCALLS]: ../resilience/#M-MOCKABLE-SYSCALLS -[M-ABSTRACTIONS-DONT-NEST]: ./#M-ABSTRACTIONS-DONT-NEST +[M-SIMPLE-ABSTRACTIONS]: ./#M-SIMPLE-ABSTRACTIONS diff --git a/src/guidelines/libs/ux/M-ESSENTIAL-FN-INHERENT.md b/src/guidelines/libs/ux/M-ESSENTIAL-FN-INHERENT.md index 6f0faee..79d3c53 100644 --- a/src/guidelines/libs/ux/M-ESSENTIAL-FN-INHERENT.md +++ b/src/guidelines/libs/ux/M-ESSENTIAL-FN-INHERENT.md @@ -39,7 +39,7 @@ impl HttpClient { // Forward calls to inherent impls. `HttpClient` can be used impl Download for HttpClient { fn download_file(&self, url: impl AsRef) { - Self::download_file(self, url) + self.download_file(url) } } ``` diff --git a/src/guidelines/libs/ux/M-INIT-CASCADED.md b/src/guidelines/libs/ux/M-INIT-CASCADED.md index ec4d58b..e3128c6 100644 --- a/src/guidelines/libs/ux/M-INIT-CASCADED.md +++ b/src/guidelines/libs/ux/M-INIT-CASCADED.md @@ -20,7 +20,7 @@ also check if [C-NEWTYPE] is applicable: ```rust, ignore # struct Deposit; # struct Account; -# struct Currency +# struct Currency; impl Deposit { // Better, signature cleaner pub fn new(account: Account, amount: Currency) -> Self { } diff --git a/src/guidelines/libs/ux/M-SERVICES-CLONE.md b/src/guidelines/libs/ux/M-SERVICES-CLONE.md index 9c2f843..b86b243 100644 --- a/src/guidelines/libs/ux/M-SERVICES-CLONE.md +++ b/src/guidelines/libs/ux/M-SERVICES-CLONE.md @@ -32,6 +32,7 @@ impl ServiceA { // If we only need to access `common` from `new` we don't have // to store it. Otherwise, make a clone we store in `Self`. let common = common.clone(); + Self { common } } } ``` @@ -43,13 +44,13 @@ Under the hood this `Clone` should **not** create a fat copy of the entire servi struct ServiceCommonInner {} #[derive(Clone)] -pub ServiceCommon { +pub struct ServiceCommon { inner: Arc } impl ServiceCommon { - pub fn new() { - Self { inner: Arc::new(ServiceCommonInner::new()) } + pub fn new() -> Self { + Self { inner: Arc::new(ServiceCommonInner {}) } } // Method forwards ... diff --git a/src/guidelines/macros/M-PROC-IMPLIED-ITEMS.md b/src/guidelines/macros/M-PROC-IMPLIED-ITEMS.md index df80c33..fb63a7b 100644 --- a/src/guidelines/macros/M-PROC-IMPLIED-ITEMS.md +++ b/src/guidelines/macros/M-PROC-IMPLIED-ITEMS.md @@ -17,7 +17,7 @@ struct UserType; struct UserType; struct ExtraType; impl UserType { - fn foo() -> ExtraType { ... }; + fn foo() -> ExtraType { ... } } ``` diff --git a/src/guidelines/performance/M-FAST-HASHER.md b/src/guidelines/performance/M-FAST-HASHER.md index eafe8f2..64ef9a0 100644 --- a/src/guidelines/performance/M-FAST-HASHER.md +++ b/src/guidelines/performance/M-FAST-HASHER.md @@ -13,5 +13,5 @@ Rust's default hasher is reasonably DoS safe on untrusted user input, but this c let lookup = HashMap::::with_capacity(1024); // Good, uses faster foldhash for internal keys. -let lookup = foldhash::HashMap::with_capacity(1024); +let lookup = foldhash::HashMap::::with_capacity(1024); ``` diff --git a/src/guidelines/performance/M-LOG-OVERHEAD.md b/src/guidelines/performance/M-LOG-OVERHEAD.md index a2e184e..f4adab0 100644 --- a/src/guidelines/performance/M-LOG-OVERHEAD.md +++ b/src/guidelines/performance/M-LOG-OVERHEAD.md @@ -22,6 +22,6 @@ for m in messages { } // Best: If possible, let telemetry users reconstruct what happened offline -log(("Processing message batch", messages.batch_id())) +log(("Processing message batch", messages.batch_id())); for m in messages { ... } ``` diff --git a/src/guidelines/performance/M-YIELD-POINTS.md b/src/guidelines/performance/M-YIELD-POINTS.md index 87911da..de886ae 100644 --- a/src/guidelines/performance/M-YIELD-POINTS.md +++ b/src/guidelines/performance/M-YIELD-POINTS.md @@ -10,7 +10,7 @@ Your future might be executed in a runtime that cannot work around blocking or l considered bad design and cause runtime overhead. If your complex task performs I/O regularly it will simply utilize these await points to preempt itself: ```rust, ignore -async fn process_items(items: &[items]) { +async fn process_items(items: &[Item]) { // Keep processing items, the runtime will preempt you automatically. for i in items { read_item(i).await; @@ -22,7 +22,7 @@ If your task performs long-running CPU operations without intermixed I/O, it sho ```rust, ignore async fn process_items(zip_file: File) { - let items = zip_file.read().async; + let items = zip_file.read().await; for i in items { decompress(i); yield_now().await; diff --git a/src/guidelines/universal/M-DOCUMENTED-MAGIC.md b/src/guidelines/universal/M-DOCUMENTED-MAGIC.md index cd57699..4185490 100644 --- a/src/guidelines/universal/M-DOCUMENTED-MAGIC.md +++ b/src/guidelines/universal/M-DOCUMENTED-MAGIC.md @@ -14,13 +14,13 @@ You should prefer named constants over inline values. ```rust, ignore // Bad: it's relatively obvious that this waits for a day, but not why -wait_timeout(60 * 60 * 24).await // Wait at most a day +wait_timeout(60 * 60 * 24).await; // Wait at most a day // Better -wait_timeout(60 * 60 * 24).await // Large enough value to ensure the server - // can finish. Setting this too low might - // make us abort a valid request. Based on - // `api.foo.com` timeout policies. +wait_timeout(60 * 60 * 24).await; // Large enough value to ensure the server + // can finish. Setting this too low might + // make us abort a valid request. Based on + // `api.foo.com` timeout policies. // Best diff --git a/src/guidelines/universal/M-STATIC-VERIFICATION.md b/src/guidelines/universal/M-STATIC-VERIFICATION.md index 707a8e5..8fe8f96 100644 --- a/src/guidelines/universal/M-STATIC-VERIFICATION.md +++ b/src/guidelines/universal/M-STATIC-VERIFICATION.md @@ -27,7 +27,6 @@ missing_debug_implementations = "warn" redundant_imports = "warn" redundant_lifetimes = "warn" trivial_numeric_casts = "warn" -unsafe_op_in_unsafe_fn = "warn" unused_lifetimes = "warn" ```