Apply suggestions from code review

Co-authored-by: Guillaume Gomez <guillaume1.gomez@gmail.com>
This commit is contained in:
Matthew Kelly 2022-08-19 09:34:20 -04:00 committed by GitHub
parent 08fa70e5c5
commit 63de1ec070
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,9 +1,9 @@
E0311 occurs when there is insufficient information for the rust compiler to This error occurs when there is insufficient information for the rust compiler to
prove that some time has a long enough lifetime. prove that some time has a long enough lifetime.
Erroneous code example: Erroneous code example:
```compile_fail, E0311 ```compile_fail,E0311
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
trait NestedBorrowMut<U, V> { trait NestedBorrowMut<U, V> {
@ -13,7 +13,7 @@ trait NestedBorrowMut<U, V> {
impl<T, U, V> NestedBorrowMut<U, V> for T impl<T, U, V> NestedBorrowMut<U, V> for T
where where
T: BorrowMut<U>, T: BorrowMut<U>,
U: BorrowMut<V>, // missing lifetime specifier here --> compile fail U: BorrowMut<V>, // error: missing lifetime specifier
{ {
fn nested_borrow_mut(&mut self) -> &mut V { fn nested_borrow_mut(&mut self) -> &mut V {
self.borrow_mut().borrow_mut() self.borrow_mut().borrow_mut()
@ -21,12 +21,11 @@ where
} }
``` ```
In this example we have a trait that borrows some inner data element of type V In this example we have a trait that borrows some inner data element of type `V`
from an outer type T, through an intermediate type U. The compiler is unable to from an outer type `T`, through an intermediate type `U`. The compiler is unable to
prove that the livetime of U is long enough to support the reference, so it prove that the livetime of `U` is long enough to support the reference. To fix the
throws E0311. To fix the issue we can explicitly add lifetime specifiers to the issue we can explicitly add lifetime specifiers to the `NestedBorrowMut` trait, which
trait, which link the lifetimes of the various data types and allow the code link the lifetimes of the various data types and allow the code to compile.
to compile.
Working implementation of the `NestedBorrowMut` trait: Working implementation of the `NestedBorrowMut` trait:
@ -40,7 +39,7 @@ trait NestedBorrowMut<'a, U, V> {
impl<'a, T, U, V> NestedBorrowMut<'a, U, V> for T impl<'a, T, U, V> NestedBorrowMut<'a, U, V> for T
where where
T: BorrowMut<U>, T: BorrowMut<U>,
U: BorrowMut<V> + 'a, U: BorrowMut<V> + 'a, // Adding lifetime specifier
{ {
fn nested_borrow_mut(&'a mut self) -> &'a mut V { fn nested_borrow_mut(&'a mut self) -> &'a mut V {
self.borrow_mut().borrow_mut() self.borrow_mut().borrow_mut()