Use split_first_mut instead of split_at_mut.

This commit is contained in:
Stephen Chung 2020-08-18 23:19:26 +08:00
parent c5360db185
commit 6a3e123306
2 changed files with 6 additions and 6 deletions

View File

@ -33,7 +33,7 @@ engine.register_raw_fn(
// But remember this is Rust, so you can keep only one mutable reference at any one time!
// Therefore, get a '&mut' reference to the first argument _last_.
// Alternatively, use `args.split_at_mut(1)` etc. to split the slice first.
// Alternatively, use `args.split_first_mut()` etc. to split the slice first.
let y: i64 = *args[1].read_lock::<i64>() // get a reference to the second argument
.unwrap(); // then copying it because it is a primary type
@ -163,15 +163,15 @@ Hold Multiple References
------------------------
In order to access a value argument that is expensive to clone _while_ holding a mutable reference
to the first argument, either _consume_ that argument via `mem::take` as above, or use `args.split_at`
to the first argument, either _consume_ that argument via `mem::take` as above, or use `args.split_first`
to partition the slice:
```rust
// Partition the slice
let (first, rest) = args.split_at_mut(1);
let (first, rest) = args.split_first_mut().unwrap();
// Mutable reference to the first parameter
let this_ptr: &mut A = &mut *first[0].write_lock::<A>().unwrap();
let this_ptr: &mut A = &mut *first.write_lock::<A>().unwrap();
// Immutable reference to the second value parameter
// This can be mutable but there is no point because the parameter is passed by value

View File

@ -521,13 +521,13 @@ impl Engine {
let result = if _is_method {
// Method call of script function - map first argument to `this`
let (first, rest) = args.split_at_mut(1);
let (first, rest) = args.split_first_mut().unwrap();
self.call_script_fn(
scope,
mods,
state,
lib,
&mut Some(first[0]),
&mut Some(*first),
fn_name,
func,
rest,