Skip to content

Commit 2b7de30

Browse files
Doc/Doctests
1 parent bf9a12c commit 2b7de30

File tree

3 files changed

+15
-50
lines changed

3 files changed

+15
-50
lines changed

README.md

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -104,46 +104,6 @@ Following are all the positive examples. They are also run by the above [GitHub
104104
For negative examples, which catch unintended `unsafe` functions/expressions/access, see
105105
documentation of each `prudent` macro.
106106

107-
# unsafe_fn
108-
```rust
109-
// @TODO both any: and default:
110-
::prudent::load!(any: "linted.rs");
111-
//::prudent::load!();
112-
113-
//use crate::prudent::*;
114-
use crate::prudent::unsafe_fn;
115-
116-
const unsafe fn unsafe_fn_no_args() {}
117-
const unsafe fn unsafe_fn_one_arg(b: bool) -> bool { b }
118-
const unsafe fn unsafe_fn_two_args(_: bool, u: u8) -> u8 { u }
119-
120-
const _: () = unsafe_fn!(unsafe_fn_no_args);
121-
const _: bool = unsafe_fn!(unsafe_fn_one_arg=> true);
122-
const _: u8 = unsafe_fn!(unsafe_fn_two_args=> true, 0);
123-
fn main() {}
124-
```
125-
126-
# unsafe_method
127-
## unsafe_method > self: shared reference
128-
```rust
129-
//::prudent::load!(any: "linted.rs");
130-
::prudent::load!();
131-
mod module {
132-
use crate::prudent::unsafe_method;
133-
// Works for Copy types
134-
const _: u8 = unsafe_method!( 1u8 =>@ unchecked_add => 0);
135-
const _: u8 = unsafe_method!(~allow_unsafe 1u8 =>@ unchecked_add => 0);
136-
const _: u8 = unsafe_method!(~allow_unsafe unsafe { 1u8.unchecked_add(2) } =>@ unchecked_add => 0);
137-
const _: u8 = unsafe_method!(~expect_unsafe unsafe { 1u8.unchecked_add(2) } =>@ unchecked_add => 0);
138-
// @TODO compile_fail:
139-
//
140-
// const _: u8 = unsafe_method!( unsafe { 1u8.unchecked_add(2) } =>@ unchecked_add => 0);
141-
//
142-
//const _: u8 = unsafe_method!(~expect_unsafe 1u8, unchecked_add, 0);
143-
}
144-
fn main() {}
145-
```
146-
147107
```rust
148108
let _todo = ();
149109
//# use prudent::unsafe_method;
@@ -168,7 +128,7 @@ impl SNonCopy {
168128

169129
fn main() {
170130
let s = SNonCopy {};
171-
// Works for non-Copy types
131+
// Works ALSO for non-Copy types
172132
unsafe_method!(s =>@ unsafe_method_no_args);
173133
unsafe_method!(s =>@ unsafe_method_one_arg => true);
174134
unsafe_method!(s =>@ unsafe_method_two_args => true, false);

src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
//! ```
2525
//!
2626
//! Pass a second parameter, after `=>`, if you want the loaded module to have name of your choice
27-
//! (other than `prudent`).
27+
//! (other than `prudent`). For example:
28+
//! ```ignore
29+
//! ::prudent::load!("../../prudent/src/linted.rs" => prudentish);
30+
//! use crate::prudentish::*;
31+
//! ```
2832
#![allow(clippy::useless_attribute)]
2933
#![allow(clippy::needless_doctest_main)]
3034
//! # Examples (linted)
@@ -94,8 +98,8 @@ macro_rules! internal_coverage_positive {
9498
) => {
9599
$crate::internal_coverage_positive!(
96100
$load_params,
97-
"unsafe_fn" -> "../coverage_positive/fn.rs",
98-
"unsafe_method > self: shared reference" -> "../coverage_positive/md-shared_ref.rs"
101+
"# unsafe_fn" -> "../coverage_positive/fn.rs",
102+
"# unsafe_method\n## unsafe_method > self: shared reference" -> "../coverage_positive/md-shared_ref.rs"
99103
)
100104
};
101105
(

src/linted.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ macro_rules! internal_prudent_unsafe_method {
247247
$( ~expect_unsafe $( { $expect_unsafe_empty_indicator } )? )?
248248
}
249249
if false {
250-
// This block "makes" owned_receiver, an instance/owned value of the same type
251-
// as $self. (Of course, the instance is invalid - this is for compile-time
250+
// This block "makes" an owned_receiver, an instance/owned value of the same
251+
// type as $self. (Of course, the instance is invalid - this is for compile-time
252252
// checks only, hence `if false {...}`.)
253253
//
254254
// Then we simulate invocation of the given method inside `unsafe {...}``, BUT
@@ -277,7 +277,7 @@ macro_rules! internal_prudent_unsafe_method {
277277
::prudent::unlinted::shared_to_mut( rref )
278278
};
279279
#[allow(unused_mut)]
280-
#[allow(invalid_value)]
280+
#[allow(invalid_value)] // for &str and other types where zeroed() issues invalid_value warning.
281281
let mut owned_receiver = ::core::mem::replace(mref, unsafe{ ::core::mem::zeroed() });
282282
// Detect code where unsafe_fn! or unsafe_method! is not needed at all. That is,
283283
// where a function/method used to be `unsafe`, but it stopped being so.
@@ -394,9 +394,10 @@ macro_rules! internal_prudent_unsafe_method_internal_build_accessors_check_args_
394394
)?
395395
let result = unsafe {
396396
// Unlike arguments, we can NOT store result of $self expression in a variable, because
397-
// it would be moved, but a method with receiver by reference `&self` or `&mut self`
398-
// does NOT move the instance it's called on. And if Self were `Copy`, then the
399-
// reference would not point to the original instance!
397+
// - it would be moved, but a method with receiver by reference `&self` or `&mut self`
398+
// does NOT move the instance it's called on. Also,
399+
// - if Self were `Copy`, then `&self` or `&mut self` reference would not point to the
400+
// original instance! (Plus extra stack used, plus lifetimes issues.)
400401
$self. $fn( $(
401402
internal_prudent_unsafe_fn_internal_access_tuple_tree_field!{ $tuple_tree, $($accessor_part),+ }
402403
),*

0 commit comments

Comments
 (0)