docs: expand the explanation for the `[unsafe]` fn tag

pull/9218/head
Delyan Angelov 2021-03-07 10:03:00 +02:00
parent 6048d0e974
commit 4c7b6ce2fd
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 18 additions and 2 deletions

View File

@ -4043,12 +4043,28 @@ fn bar() {
foo() // will not be called if `-d debug` is not passed
}
// Calls to this function must be in unsafe{} blocks
// Calls to following function must be in unsafe{} blocks.
// Note that the code in the body of `risky_business()` will still be
// checked, unless you also wrap it in `unsafe {}` blocks.
// This is usefull, when you want to have an `[unsafe]` function that
// has checks before/after a certain unsafe operation, that will still
// benefit from V's safety features.
[unsafe]
fn risky_business() {
// code that will be checked, perhaps checking pre conditions
unsafe {
// code that *will not be* checked, like pointer arithmetic,
// accessing union fields, calling other `[unsafe]` fns, etc...
// Usually, it is a good idea to try minimizing code wrapped
// in unsafe{} as much as possible.
// See also [Memory-unsafe code](#memory-unsafe-code)
}
// code that will be checked, perhaps checking post conditions and/or
// keeping invariants
}
// V's autofree engine will not take care of memory management in this function
// V's autofree engine will not take care of memory management in this function.
// You will have the responsibility to free memory manually yourself in it.
[manualfree]
fn custom_allocations() {
}