diff --git a/ruma-signatures/src/functions.rs b/ruma-signatures/src/functions.rs index 8bc7b521..cafa21e8 100644 --- a/ruma-signatures/src/functions.rs +++ b/ruma-signatures/src/functions.rs @@ -134,27 +134,25 @@ where let mut signature_map; let maybe_unsigned; - // Pull `signatures` and `unsigned` out of the object, and limit the scope of the mutable - // borrow of `value` so we can call `to_string` with it below. - { - let map = match value { - Value::Object(ref mut map) => map, - _ => return Err(Error::new("JSON value must be a JSON object")), - }; + // Pull `signatures` and `unsigned` out of the object. + let map = match value { + Value::Object(map) => map, + _ => return Err(Error::new("JSON value must be a JSON object")), + }; - signature_map = match map.remove("signatures") { - Some(signatures_value) => match signatures_value.as_object() { - Some(signatures) => from_value(Value::Object(signatures.clone()))?, - None => return Err(Error::new("field `signatures` must be a JSON object")), - }, - None => BTreeMap::new(), - }; + // FIXME: Once MSRV >= 1.45.0, use remove_key and don't allocate new `String`s below. + signature_map = match map.remove("signatures") { + Some(signatures_value) => match signatures_value.as_object() { + Some(signatures) => from_value(Value::Object(signatures.clone()))?, + None => return Err(Error::new("field `signatures` must be a JSON object")), + }, + None => BTreeMap::new(), + }; - maybe_unsigned = map.remove("unsigned"); - } + maybe_unsigned = map.remove("unsigned"); // Get the canonical JSON. - let json = to_string(&value)?; + let json = to_string(map)?; // Sign the canonical JSON. let signature = key_pair.sign(json.as_bytes()); @@ -164,9 +162,6 @@ where signature_set.insert(signature.id(), signature.base64()); - // Safe to unwrap because we did this exact check at the beginning of the function. - let map = value.as_object_mut().unwrap(); - // Put `signatures` and `unsigned` back in. map.insert("signatures".into(), to_value(signature_map)?);