refactor(sdk-ui): remove expect() with better borrow#6513
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6513 +/- ##
=======================================
Coverage 89.87% 89.87%
=======================================
Files 381 381
Lines 105781 105758 -23
Branches 105781 105758 -23
=======================================
- Hits 95071 95051 -20
- Misses 7048 7051 +3
+ Partials 3662 3656 -6 ☔ View full report in Codecov by Sentry. |
Hywan
left a comment
There was a problem hiding this comment.
See my proposal to avoid allocating the debug_string for nothing if event is a poll.
| fn poll_state_from_item<'a>( | ||
| event: &'a mut Cow<'_, EventTimelineItem>, | ||
| ) -> Result<&'a mut PollState, AggregationError> { | ||
| if event.content().is_poll() { |
There was a problem hiding this comment.
Not sure if the following works but it seems ok:
if let TimelineItemContent::MsgLike(MsgLikeContent { kind: MsgLikeKind::Poll(state), ..}) = event.to_mut().content_mut() {
Ok(state)
} else {
Err(AggregationError::InvalidType {
expected: "a poll".to_owned(),
actual: event.content().debug_string().to_owned(),
})
}There was a problem hiding this comment.
You will have the borrow issue :/ event.to_mut() borrows event mutably for the lifetime 'a so for all the function scope, thus you cannot borrow again in the else
There was a problem hiding this comment.
I think this is what is described here? https://rust-lang.github.io/rfcs/2094-nll.html#problem-case-3-conditional-control-flow-across-functions
There was a problem hiding this comment.
I've tested the following and it works:
let content = event.to_mut().content_mut();
if let TimelineItemContent::MsgLike(MsgLikeContent { kind: MsgLikeKind::Poll(state), .. }) =
content
{
Ok(state)
} else {
Err(AggregationError::InvalidType {
expected: "a poll".to_owned(),
actual: content.debug_string().to_owned(),
})
}expect() with better borrow
Follow up on review on this PR #6494 (comment)
Remove the usage of if/expect that was introduced because of a borrowing problem.
Instead we use
ok_or_else, but we clone the debug_string early (to avoid needing to borrow event again in theok_or_else), it is not ideal either but better that the expect/panicI also removed the
is_rtc_notificationandis_live_locationthat was created I believe just to avoid the borrowing problem? They were only used in test and it just a shortcut formatches!()CHANGELOG.mdfiles.Signed-off-by: