Skip to content

Commit

Permalink
add narrative and break things
Browse files Browse the repository at this point in the history
  • Loading branch information
brayniac committed Jun 6, 2024
1 parent 6fd1cb0 commit 36998e2
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/clients/momento/commands/sorted_set_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub async fn sorted_set_add(
if request.members.len() == 1 {
let (member, score) = request.members.first().unwrap();

// here we are able to borrow from the Arc<[u8]> and get a &[u8]
// we deref the &f64 to get a f64. It's Copy, so that's fine. Members
// might be long slices, so avoiding a clone is very nice.

let r = SortedSetPutElementRequest::new(cache_name, &*request.key, &**member, *score)
.ttl(CollectionTtl::new(request.ttl, false));

Expand All @@ -28,10 +32,30 @@ pub async fn sorted_set_add(

record_result!(result, SORTED_SET_ADD)
} else {
let d: Vec<(Vec<u8>, f64)> = request
// there's a couple of ways we can do this, but essentially we need to
// transform our Arc<[u8]> into something that implements Into<Vec<u8>>
// our obvious options are to just convert to_vec() to get a Vec<u8>,
// but this requires copying the members.

// this works, we use IntoIter because we can. It doesn't really make
// much of a difference

// let d: Vec<(Vec<u8>, f64)> = request
// .members
// .into_iter()
// .map(|(m, s)| (m.to_vec(), s))
// .collect();

// the other way would be like how we do above, we can borrow from the
// Arc<[u8]> to get a &[u8]. Since we're just going to borrow, we must
// use plain iter() to get borrows from the Arc<T> that's still in the
// `SortedSetAdd` request.

// this does not work!
let d: Vec<(&[u8], f64)> = request
.members
.into_iter()
.map(|(m, s)| (m.to_vec(), s))
.iter()
.map(|(m, s)| (&**m, *s))
.collect();

let r = SortedSetPutElementsRequest::new(cache_name, &*request.key, d)
Expand Down

0 comments on commit 36998e2

Please sign in to comment.