Rollup merge of #79486 - camelid:E0591-code-cleanup, r=lcnr

Slightly improve code samples in E0591

* Improve formatting
* Don't hide `unsafe` block - it's important!
This commit is contained in:
Jonas Schievink 2020-11-28 15:58:30 +01:00 committed by GitHub
commit 208d680f77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 12 deletions

View File

@ -1,14 +1,20 @@
Per [RFC 401][rfc401], if you have a function declaration `foo`: Per [RFC 401][rfc401], if you have a function declaration `foo`:
``` ```
struct S;
// For the purposes of this explanation, all of these // For the purposes of this explanation, all of these
// different kinds of `fn` declarations are equivalent: // different kinds of `fn` declarations are equivalent:
struct S;
fn foo(x: S) { /* ... */ } fn foo(x: S) { /* ... */ }
# #[cfg(for_demonstration_only)] # #[cfg(for_demonstration_only)]
extern "C" { fn foo(x: S); } extern "C" {
fn foo(x: S);
}
# #[cfg(for_demonstration_only)] # #[cfg(for_demonstration_only)]
impl S { fn foo(self) { /* ... */ } } impl S {
fn foo(self) { /* ... */ }
}
``` ```
the type of `foo` is **not** `fn(S)`, as one might expect. the type of `foo` is **not** `fn(S)`, as one might expect.
@ -40,10 +46,10 @@ extern "C" fn foo(userdata: Box<i32>) {
# fn callback(_: extern "C" fn(*mut i32)) {} # fn callback(_: extern "C" fn(*mut i32)) {}
# use std::mem::transmute; # use std::mem::transmute;
# unsafe { unsafe {
let f: extern "C" fn(*mut i32) = transmute(foo); let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f); callback(f);
# } }
``` ```
Here, transmute is being used to convert the types of the fn arguments. Here, transmute is being used to convert the types of the fn arguments.

View File

@ -1,12 +1,18 @@
Per [RFC 401][rfc401], if you have a function declaration `foo`: Per [RFC 401][rfc401], if you have a function declaration `foo`:
``` ```
struct S;
// For the purposes of this explanation, all of these // For the purposes of this explanation, all of these
// different kinds of `fn` declarations are equivalent: // different kinds of `fn` declarations are equivalent:
struct S;
fn foo(x: S) { /* ... */ } fn foo(x: S) { /* ... */ }
extern "C" { fn foo(x: S); } extern "C" {
impl S { fn foo(self) { /* ... */ } } fn foo(x: S);
}
impl S {
fn foo(self) { /* ... */ }
}
``` ```
the type of `foo` is **not** `fn(S)`, as one might expect. the type of `foo` is **not** `fn(S)`, as one might expect.
@ -34,8 +40,10 @@ extern "C" fn foo(userdata: Box<i32>) {
/* ... */ /* ... */
} }
let f: extern "C" fn(*mut i32) = transmute(foo); unsafe {
callback(f); let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);
}
``` ```
Here, transmute is being used to convert the types of the fn arguments. Here, transmute is being used to convert the types of the fn arguments.