Remove unnecessary Option-wrapping and successive unwrapping

This commit is contained in:
Jonas Platte 2021-06-17 20:44:29 +02:00
parent b291e76572
commit 808741bcb6
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
3 changed files with 33 additions and 50 deletions

View file

@ -160,15 +160,12 @@ pub async fn get_public_rooms_filtered_helper(
.map(|c| { .map(|c| {
// Convert ruma::api::federation::directory::get_public_rooms::v1::PublicRoomsChunk // Convert ruma::api::federation::directory::get_public_rooms::v1::PublicRoomsChunk
// to ruma::api::client::r0::directory::PublicRoomsChunk // to ruma::api::client::r0::directory::PublicRoomsChunk
Ok::<_, Error>( serde_json::from_str(
serde_json::from_str( &serde_json::to_string(&c)
&serde_json::to_string(&c) .expect("PublicRoomsChunk::to_string always works"),
.expect("PublicRoomsChunk::to_string always works"),
)
.expect("federation and client-server PublicRoomsChunk are the same type"),
) )
.expect("federation and client-server PublicRoomsChunk are the same type")
}) })
.filter_map(|r| r.ok())
.collect(), .collect(),
prev_batch: response.prev_batch, prev_batch: response.prev_batch,
next_batch: response.next_batch, next_batch: response.next_batch,

View file

@ -2023,39 +2023,33 @@ impl Rooms {
.map(str::to_lowercase) .map(str::to_lowercase)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let iterators = words let iterators = words.clone().into_iter().map(move |word| {
.clone() let mut prefix2 = prefix.clone();
.into_iter() prefix2.extend_from_slice(word.as_bytes());
.map(move |word| { prefix2.push(0xff);
let mut prefix2 = prefix.clone();
prefix2.extend_from_slice(word.as_bytes());
prefix2.push(0xff);
let mut last_possible_id = prefix2.clone(); let mut last_possible_id = prefix2.clone();
last_possible_id.extend_from_slice(&u64::MAX.to_be_bytes()); last_possible_id.extend_from_slice(&u64::MAX.to_be_bytes());
Ok::<_, Error>( self.tokenids
self.tokenids .iter_from(&last_possible_id, true) // Newest pdus first
.iter_from(&last_possible_id, true) // Newest pdus first .take_while(move |(k, _)| k.starts_with(&prefix2))
.take_while(move |(k, _)| k.starts_with(&prefix2)) .map(|(key, _)| {
.map(|(key, _)| { let pduid_index = key
let pduid_index = key .iter()
.iter() .enumerate()
.enumerate() .filter(|(_, &b)| b == 0xff)
.filter(|(_, &b)| b == 0xff) .nth(1)
.nth(1) .ok_or_else(|| Error::bad_database("Invalid tokenid in db."))?
.ok_or_else(|| Error::bad_database("Invalid tokenid in db."))? .0
.0 + 1; // +1 because the pdu id starts AFTER the separator
+ 1; // +1 because the pdu id starts AFTER the separator
let pdu_id = key[pduid_index..].to_vec(); let pdu_id = key[pduid_index..].to_vec();
Ok::<_, Error>(pdu_id) Ok::<_, Error>(pdu_id)
}) })
.filter_map(|r| r.ok()), .filter_map(|r| r.ok())
) });
})
.filter_map(|r| r.ok());
Ok(( Ok((
utils::common_elements(iterators, |a, b| { utils::common_elements(iterators, |a, b| {

View file

@ -532,15 +532,11 @@ pub async fn get_public_rooms_filtered_route(
.map(|c| { .map(|c| {
// Convert ruma::api::federation::directory::get_public_rooms::v1::PublicRoomsChunk // Convert ruma::api::federation::directory::get_public_rooms::v1::PublicRoomsChunk
// to ruma::api::client::r0::directory::PublicRoomsChunk // to ruma::api::client::r0::directory::PublicRoomsChunk
Ok::<_, Error>( serde_json::from_str(
serde_json::from_str( &serde_json::to_string(&c).expect("PublicRoomsChunk::to_string always works"),
&serde_json::to_string(&c)
.expect("PublicRoomsChunk::to_string always works"),
)
.expect("federation and client-server PublicRoomsChunk are the same type"),
) )
.expect("federation and client-server PublicRoomsChunk are the same type")
}) })
.filter_map(|r| r.ok())
.collect(), .collect(),
prev_batch: response.prev_batch, prev_batch: response.prev_batch,
next_batch: response.next_batch, next_batch: response.next_batch,
@ -580,15 +576,11 @@ pub async fn get_public_rooms_route(
.map(|c| { .map(|c| {
// Convert ruma::api::federation::directory::get_public_rooms::v1::PublicRoomsChunk // Convert ruma::api::federation::directory::get_public_rooms::v1::PublicRoomsChunk
// to ruma::api::client::r0::directory::PublicRoomsChunk // to ruma::api::client::r0::directory::PublicRoomsChunk
Ok::<_, Error>( serde_json::from_str(
serde_json::from_str( &serde_json::to_string(&c).expect("PublicRoomsChunk::to_string always works"),
&serde_json::to_string(&c)
.expect("PublicRoomsChunk::to_string always works"),
)
.expect("federation and client-server PublicRoomsChunk are the same type"),
) )
.expect("federation and client-server PublicRoomsChunk are the same type")
}) })
.filter_map(|r| r.ok())
.collect(), .collect(),
prev_batch: response.prev_batch, prev_batch: response.prev_batch,
next_batch: response.next_batch, next_batch: response.next_batch,