|
2 | 2 | results](https://github.com/peter-lyons-kehl/prudent/actions/workflows/main.yml/badge.svg)](https://github.com/peter-lyons-kehl/prudent/actions) |
3 | 3 |
|
4 | 4 | # Summary |
5 | | -`prudent` helps you minimize the amount of Rust code that is marked as `unsafe`. |
| 5 | +`prudent` helps you minimize/isolate parts of Rust `unsafe` expressions/statements. |
6 | 6 |
|
7 | 7 | - ergonomic (as much as possible) |
8 | | -- obvious |
9 | | -- lightweight (no dependencies, no procedural macros - fast build) |
| 8 | +- lightweight (no dependencies, no procedural macros - fast build, `rust-analyzer`-friendly) |
10 | 9 | - zero-cost (for binary size, speed and memory), verified in compile time |
11 | 10 |
|
12 | | -# const-friendly |
13 | | -Results of `prudent`'s macro invocations are `const` (if the original invocation/expression would |
14 | | -also be `const`). |
15 | | - |
16 | | -# Lint-like check for unsafe_method |
17 | | - |
18 | | -# Quality assurance |
| 11 | +# API and examples |
| 12 | +Following are all the positive examples. They are also run by the above [GitHub Actions] as |
| 13 | +[doctests](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html). |
19 | 14 |
|
20 | | -Checks and tests are run by [GitHub Actions]. See |
21 | | -[results](https://github.com/peter-lyons-kehl/prudent/actions). All tests <!--scripts--> run on |
22 | | -Alpine Linux (without `libc`, in a `rust:1.87-alpine` container)<!-- and are POSIX-compliant-->: |
| 15 | +For negative examples, see documentation of each `prudent` macro. |
23 | 16 |
|
24 | | -- `rustup component add clippy rustfmt` |
25 | | -- `cargo clippy` |
26 | | -- `cargo fmt --check` |
27 | | -- `cargo doc --no-deps --quiet` |
28 | | -- `cargo test` |
29 | | -- `cargo test --release` |
30 | | -- with [`MIRI`] |
31 | | - - `rustup install nightly --profile minimal` |
32 | | - - `rustup +nightly component add miri` |
33 | | - - `cargo +nightly miri test` |
| 17 | +## unsafe_fn |
| 18 | +```rust |
| 19 | +use prudent::unsafe_fn; |
34 | 20 |
|
35 | | -## Verification of expected errors |
36 | | -- Error code validation: Where possible, expected error numbers are validated with `cargo +nightly |
37 | | - test`, ([The Rustdoc book > Unstable features > Error numbers for compile-fail |
38 | | - doctests](https://doc.rust-lang.org/rustdoc/unstable-features.html#error-numbers-for-compile-fail-doctests)). |
39 | | - The error codes are validated by [GitHub Actions](.github/workflows/main.yml), see |
40 | | - [results](https://github.com/prudent-rs/prudent/actions). Error code validation requires `nightly` |
41 | | - Rust toolchain. See also [`src/linted_with_tests.rs`](src/linted_with_tests.rs) for expected |
42 | | - compilation error codes. |
43 | | -- Error output validation: Some lint violations don't have a special error code. So we validate the |
44 | | - error message in |
45 | | - [violations_coverage/verify_error_messages/](violations_coverage/verify_error_messages/) with |
46 | | - [dtolnay/trybuild](https://github.com/dtolnay/trybuild/) |
47 | | - [crates.io/crates/trybuild](https://crates.io/crates/trybuild). |
| 21 | +const unsafe fn unsafe_fn_no_args() {} |
| 22 | +const unsafe fn unsafe_fn_one_arg(b: bool) -> bool { b } |
| 23 | +const unsafe fn unsafe_fn_two_args(_: bool, u: u8) -> u8 { u } |
48 | 24 |
|
49 | | -# API and examples |
50 | | -Following are all the positive examples. They are also run by the above [GitHub Actions] as |
51 | | -[doctests](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html). |
| 25 | +const _: () = unsafe_fn!(unsafe_fn_no_args); |
| 26 | +const _: bool = unsafe_fn!(unsafe_fn_one_arg=> true); |
| 27 | +const _: u8 = unsafe_fn!(unsafe_fn_two_args=> true, 0); |
| 28 | +fn main() {} |
| 29 | +``` |
52 | 30 |
|
53 | | -For negative examples, which catch unintended `unsafe` functions/expressions/access, see |
54 | | -documentation of each `prudent` macro. |
| 31 | +## unsafe_method |
55 | 32 |
|
| 33 | +### self by value |
56 | 34 | ```rust |
57 | | -let _todo = (); |
58 | | -//# use prudent::unsafe_method; |
59 | | -//const _: u8 = unsafe_method!(~expect_unsafe ~allow_unsafe 1u8, unchecked_add, 0); |
| 35 | +use prudent::unsafe_method; |
| 36 | +const _: u8 = unsafe_method!( 1u8 =>@ unchecked_add => 0 ); |
60 | 37 | ``` |
61 | 38 |
|
62 | 39 | ```rust |
@@ -425,6 +402,36 @@ fn main() { |
425 | 402 | } |
426 | 403 | ``` |
427 | 404 |
|
| 405 | +# const-friendly |
| 406 | +Results of `prudent`'s macro invocations are `const` (if the original invocation/expression would |
| 407 | +also be `const`). |
| 408 | + |
| 409 | +# Quality assurance |
| 410 | +## Continuous integration |
| 411 | +[GitHub Actions] runs on Alpine Linux (without `libc`). See [the |
| 412 | +reports](https://github.com/peter-lyons-kehl/prudent/actions). |
| 413 | + |
| 414 | +- `cargo test` |
| 415 | +- `cargo clippy` for linting |
| 416 | +- [`MIRI`] |
| 417 | + |
| 418 | +## Verification of expected errors |
| 419 | +- Error code validation: Where possible, expected error numbers are validated with `cargo +nightly |
| 420 | + test`, ([The Rustdoc book > Unstable features > Error numbers for compile-fail |
| 421 | + doctests](https://doc.rust-lang.org/rustdoc/unstable-features.html#error-numbers-for-compile-fail-doctests)). |
| 422 | + The error codes are validated by [GitHub Actions](.github/workflows/main.yml), see |
| 423 | + [results](https://github.com/prudent-rs/prudent/actions). Error code validation requires `nightly` |
| 424 | + Rust toolchain. See also [`src/linted_with_tests.rs`](src/linted_with_tests.rs) for expected |
| 425 | + compilation error codes. |
| 426 | +- Error output validation: Some lint violations don't have a special error code. So we validate the |
| 427 | + error message in |
| 428 | + [violations_coverage/verify_error_messages/](violations_coverage/verify_error_messages/) with |
| 429 | + [dtolnay/trybuild](https://github.com/dtolnay/trybuild/) |
| 430 | + [crates.io/crates/trybuild](https://crates.io/crates/trybuild). |
| 431 | + |
| 432 | +## unsafe_method |
| 433 | +TODO |
| 434 | + |
428 | 435 | # Details |
429 | 436 |
|
430 | 437 | `prudent` helps both authors, reviewers and all of us: |
@@ -467,16 +474,21 @@ However, |
467 | 474 |
|
468 | 475 | `prudent` is `no-std`-compatible. It doesn't need allocation either. |
469 | 476 |
|
| 477 | +All of `prudent`'s "positive" functionality works on `stable` Rust (minimum version 1.39). However, |
| 478 | +if you use `assert_unsafe_methods` feature to verify that `unsafe_method` is applied only to methods |
| 479 | +that are indeed `unsafe`, that requires |
| 480 | +- `nightly`, and |
| 481 | +- access to set `RUSTFLAGS="-Znext-solver=globally"`. That is unfortunately not possible for |
| 482 | + doctests. See, and give thumbs up to, [Related issues](#Related-issues). |
| 483 | + |
470 | 484 | ## Always forward compatible |
471 | 485 |
|
472 | | -`prudent` is planned to be always below version `1.0`. So <!--stable (**even**-numbered) versions--> |
473 | | -it will be forward compatible. (If a need ever arises for big incompatibility, that can go to a new |
474 | | -crate.) |
| 486 | +`prudent` is planned to be forward compatible. (If a need ever arises for big incompatibility, that |
| 487 | +can go to a new crate.) |
475 | 488 |
|
476 | | -That allows you to specify `prudent` as a dependency with version `0.*`, which will match ANY |
477 | | -**major** versions (below `1.0`, of course). That will match the newest <!-- (**even**-numbered |
478 | | -major)--> |
479 | | -<!-- stable--> version (available for your Rust) automatically. |
| 489 | +That allows `prudent` to be always below version `1.0`. Then you can specify `prudent` as a |
| 490 | +dependency with version `0.*`, which will match ANY **major** versions (below `1.0`, of course). |
| 491 | +That will match the newest version (available for your Rust) automatically. |
480 | 492 |
|
481 | 493 | This is special only to `0.*` - it is **not** possible to have a wildcard matching various **major** |
482 | 494 | versions `1.0` or higher. |
|
0 commit comments