fn producer_buf(producer: &mut ringbuf::HeapProd<u8>, len: usize) -> &mut [MaybeUninit<u8>] { loop { let (inbuf, _) = producer.vacant_slices_mut(); if inbuf.len() >= len { // let (inbuf, _) = producer.vacant_slices_mut(); // fetch again is ok return inbuf; } std::hint::spin_loop(); } } 

The above code gets error:

281 | fn producer_buf(producer: &mut HeapProd<u8>, len: usize) -> &mut [MaybeUninit<u8>] { | - let's call the lifetime of this reference `'1` ... 284 | let (inbuf, _) = producer.vacant_slices_mut(); | ^^^^^^^^ `*producer` was mutably borrowed here in the previous iteration of the loop ... 287 | return inbuf; | ----- returning this value requires that `*producer` is borrowed for `'1` For more information about this error, try `rustc --explain E0499`. 

It becomes OK if adding the commented line, to fetch the inbuf again.

Why the original code is not OK?

2 posts - 2 participants

Read full topic