On 11-10-22, 12:25, Kent Gibson wrote:
On Tue, Oct 11, 2022 at 09:46:51AM +0530, Viresh Kumar wrote:
On 29-09-22, 15:55, Miguel Ojeda wrote:
It looks like a container whose elements get invalidated, so `read_edge_event` could require an exclusive reference to `buffer` in Rust, that way you cannot keep borrows to its elements like `ev` if you want to call it. But of course this requires tying the lifetime of the events to that of the buffer.
What about the below code changes on top of V6 ?
Can you clone the event to detach it from the buffer?
I thought we can always do:
event.event_clone() to get a copy, which won't have a reference to the Buffer. This is how it is defined:
pub fn event_clone(&self) -> Result<Self> { let event = unsafe { gpiod::gpiod_edge_event_copy(self.event) }; if event.is_null() { return Err(Error::OperationFailed( OperationType::EdgeEventCopy, Errno::last(), )); }
Ok(Self { buffer: None, event, }) }
But when I try to do this in the earlier example:
@@ -40,10 +40,12 @@ fn main() -> Result<()> { let event1 = buffer.event(1)?;
println!("{:?}", (event0.line_offset(), event1.line_offset())); + let event0_copy = event0.event_clone()?; drop(event0); // This fails to compile // request.read_edge_events(&mut buffer)?; drop(event1); request.read_edge_events(&mut buffer)?; + drop(event0_copy); } }
compilation fails :(
error[E0502]: cannot borrow `buffer` as mutable because it is also borrowed as immutable --> libgpiod/examples/gpiobufferevent.rs:48:34 | 39 | let event0 = buffer.event(0)?; | --------------- immutable borrow occurs here ... 48 | request.read_edge_events(&mut buffer)?; | ^^^^^^^^^^^ mutable borrow occurs here 49 | drop(event0_copy); | ----------- immutable borrow later used here
And I am not sure why, as the reference isn't used anymore in this case to the event0.