[fractal] Backend: Change function signatures to mutable vectors instead of cloning internally



commit 47fdc3f58e9261852d5e844fa62852ddeb4f1aa8
Author: Alejandro Domínguez <adomu net-c com>
Date:   Sun Dec 30 02:18:05 2018 +0100

    Backend: Change function signatures to mutable vectors instead of cloning internally

 fractal-matrix-api/src/backend/directory.rs |  2 +-
 fractal-matrix-api/src/backend/mod.rs       |  7 ++---
 fractal-matrix-api/src/backend/register.rs  |  6 ++--
 fractal-matrix-api/src/backend/room.rs      | 45 +++++++++++++++++------------
 fractal-matrix-api/src/backend/stickers.rs  | 24 +++++++--------
 fractal-matrix-api/src/backend/sync.rs      |  2 +-
 fractal-matrix-api/src/backend/user.rs      | 22 +++++++-------
 7 files changed, 57 insertions(+), 51 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/directory.rs b/fractal-matrix-api/src/backend/directory.rs
index 5872eeb6..6b09bc02 100644
--- a/fractal-matrix-api/src/backend/directory.rs
+++ b/fractal-matrix-api/src/backend/directory.rs
@@ -77,7 +77,7 @@ pub fn room_search(
         params.push(("server", hs));
     }
 
-    let url = bk.url("publicRooms", &params)?;
+    let url = bk.url("publicRooms", params)?;
     let base = bk.get_base_url()?;
 
     let mut attrs = json!({ "limit": globals::ROOM_DIRECTORY_LIMIT });
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index 0d75f7d8..d46e16ea 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -58,14 +58,13 @@ impl Backend {
         Ok(url)
     }
 
-    fn url(&self, path: &str, params: &[(&str, String)]) -> Result<Url, Error> {
+    fn url(&self, path: &str, mut params: Vec<(&str, String)>) -> Result<Url, Error> {
         let base = self.get_base_url()?;
         let tk = self.data.lock().unwrap().access_token.clone();
 
-        let mut params2 = params.to_vec();
-        params2.push(("access_token", tk.clone()));
+        params.push(("access_token", tk));
 
-        client_url(&base, path, &params2)
+        client_url(&base, path, &params)
     }
 
     pub fn run(mut self) -> Sender<BKCommand> {
diff --git a/fractal-matrix-api/src/backend/register.rs b/fractal-matrix-api/src/backend/register.rs
index 149d5823..fa33cd86 100644
--- a/fractal-matrix-api/src/backend/register.rs
+++ b/fractal-matrix-api/src/backend/register.rs
@@ -69,7 +69,7 @@ fn build_login_attrs(user: &str, password: &str) -> Result<JsonValue, Error> {
 pub fn login(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(), Error> {
     let s = String::from(server);
     bk.data.lock().unwrap().server_url = s;
-    let url = bk.url("login", &[])?;
+    let url = bk.url("login", vec![])?;
 
     let attrs = build_login_attrs(user, password)?;
     let data = bk.data.clone();
@@ -111,7 +111,7 @@ pub fn set_token(bk: &Backend, token: String, uid: String, server: &str) -> Resu
 }
 
 pub fn logout(bk: &Backend) -> Result<(), Error> {
-    let url = bk.url("logout", &[])?;
+    let url = bk.url("logout", vec![])?;
     let attrs = json!({});
 
     let data = bk.data.clone();
@@ -133,7 +133,7 @@ pub fn logout(bk: &Backend) -> Result<(), Error> {
 pub fn register(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(), Error> {
     let s = String::from(server);
     bk.data.lock().unwrap().server_url = s;
-    let url = bk.url("register", &vec![("kind", String::from("user"))])?;
+    let url = bk.url("register", vec![("kind", String::from("user"))])?;
 
     let attrs = json!({
         "auth": {"type": "m.login.password"},
diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs
index 5c2f48fe..573eaddf 100644
--- a/fractal-matrix-api/src/backend/room.rs
+++ b/fractal-matrix-api/src/backend/room.rs
@@ -38,7 +38,7 @@ pub fn set_room(bk: &Backend, id: String) -> Result<(), Error> {
 }
 
 pub fn get_room_detail(bk: &Backend, roomid: String, key: String) -> Result<(), Error> {
-    let url = bk.url(&format!("rooms/{}/state/{}", roomid, key), &[])?;
+    let url = bk.url(&format!("rooms/{}/state/{}", roomid, key), vec![])?;
 
     let tx = bk.tx.clone();
     let keys = key.clone();
@@ -60,7 +60,7 @@ pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
     let userid = bk.data.lock().unwrap().user_id.clone();
     let baseu = bk.get_base_url()?;
     let tk = bk.data.lock().unwrap().access_token.clone();
-    let url = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), &[])?;
+    let url = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), vec![])?;
 
     let tx = bk.tx.clone();
     get!(
@@ -96,7 +96,7 @@ pub fn get_room_avatar(bk: &Backend, roomid: String) -> Result<(), Error> {
 }
 
 pub fn get_room_members(bk: &Backend, roomid: String) -> Result<(), Error> {
-    let url = bk.url(&format!("rooms/{}/joined_members", roomid), &[])?;
+    let url = bk.url(&format!("rooms/{}/joined_members", roomid), vec![])?;
 
     let tx = bk.tx.clone();
     get!(
@@ -132,7 +132,7 @@ pub fn get_room_messages(bk: &Backend, roomid: String, from: String) -> Result<(
             "{ \"types\": [\"m.room.message\", \"m.sticker\"] }".to_string(),
         ),
     ];
-    let url = bk.url(&format!("rooms/{}/messages", roomid), &params)?;
+    let url = bk.url(&format!("rooms/{}/messages", roomid), params)?;
     let tx = bk.tx.clone();
     get!(
         &url,
@@ -240,7 +240,10 @@ pub fn send_msg(bk: &Backend, msg: Message) -> Result<(), Error> {
     let roomid = msg.room.clone();
 
     let id = msg.id.unwrap_or_default();
-    let url = bk.url(&format!("rooms/{}/send/m.room.message/{}", roomid, id), &[])?;
+    let url = bk.url(
+        &format!("rooms/{}/send/m.room.message/{}", roomid, id),
+        vec![],
+    )?;
 
     let mut attrs = json!({
         "body": msg.body.clone(),
@@ -287,7 +290,10 @@ pub fn redact_msg(bk: &Backend, msg: &Message) -> Result<(), Error> {
     let msgid = msg.id.clone().unwrap_or_default();
     let txnid = msg.get_txn_id();
 
-    let url = bk.url(&format!("rooms/{}/redact/{}/{}", roomid, msgid, txnid), &[])?;
+    let url = bk.url(
+        &format!("rooms/{}/redact/{}/{}", roomid, msgid, txnid),
+        vec![],
+    )?;
 
     let attrs = json!({
         "reason": "Deletion requested by the sender"
@@ -315,7 +321,7 @@ pub fn redact_msg(bk: &Backend, msg: &Message) -> Result<(), Error> {
 }
 
 pub fn join_room(bk: &Backend, roomid: String) -> Result<(), Error> {
-    let url = bk.url(&format!("join/{}", urlencoding::encode(&roomid)), &[])?;
+    let url = bk.url(&format!("join/{}", urlencoding::encode(&roomid)), vec![])?;
 
     let tx = bk.tx.clone();
     let data = bk.data.clone();
@@ -334,7 +340,7 @@ pub fn join_room(bk: &Backend, roomid: String) -> Result<(), Error> {
 }
 
 pub fn leave_room(bk: &Backend, roomid: &str) -> Result<(), Error> {
-    let url = bk.url(&format!("rooms/{}/leave", roomid), &[])?;
+    let url = bk.url(&format!("rooms/{}/leave", roomid), vec![])?;
 
     let tx = bk.tx.clone();
     post!(
@@ -351,7 +357,10 @@ pub fn leave_room(bk: &Backend, roomid: &str) -> Result<(), Error> {
 }
 
 pub fn mark_as_read(bk: &Backend, roomid: &str, eventid: &str) -> Result<(), Error> {
-    let url = bk.url(&format!("rooms/{}/receipt/m.read/{}", roomid, eventid), &[])?;
+    let url = bk.url(
+        &format!("rooms/{}/receipt/m.read/{}", roomid, eventid),
+        vec![],
+    )?;
 
     let tx = bk.tx.clone();
     let r = String::from(roomid);
@@ -370,7 +379,7 @@ pub fn mark_as_read(bk: &Backend, roomid: &str, eventid: &str) -> Result<(), Err
     // This event API call isn't in the current doc but I found this in the
     // matrix-js-sdk
     // https://github.com/matrix-org/matrix-js-sdk/blob/master/src/base-apis.js#L851
-    let url = bk.url(&format!("rooms/{}/read_markers", roomid), &[])?;
+    let url = bk.url(&format!("rooms/{}/read_markers", roomid), vec![])?;
     let attrs = json!({
         "m.fully_read": eventid,
         "m.read": json!(null),
@@ -381,7 +390,7 @@ pub fn mark_as_read(bk: &Backend, roomid: &str, eventid: &str) -> Result<(), Err
 }
 
 pub fn set_room_name(bk: &Backend, roomid: &str, name: &str) -> Result<(), Error> {
-    let url = bk.url(&format!("rooms/{}/state/m.room.name", roomid), &[])?;
+    let url = bk.url(&format!("rooms/{}/state/m.room.name", roomid), vec![])?;
 
     let attrs = json!({
         "name": name,
@@ -404,7 +413,7 @@ pub fn set_room_name(bk: &Backend, roomid: &str, name: &str) -> Result<(), Error
 }
 
 pub fn set_room_topic(bk: &Backend, roomid: &str, topic: &str) -> Result<(), Error> {
-    let url = bk.url(&format!("rooms/{}/state/m.room.topic", roomid), &[])?;
+    let url = bk.url(&format!("rooms/{}/state/m.room.topic", roomid), vec![])?;
 
     let attrs = json!({
         "topic": topic,
@@ -431,7 +440,7 @@ pub fn set_room_avatar(bk: &Backend, roomid: &str, avatar: &str) -> Result<(), E
     let tk = bk.data.lock().unwrap().access_token.clone();
     let params = vec![("access_token", tk.clone())];
     let mediaurl = media_url(&baseu, "upload", &params)?;
-    let roomurl = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), &[])?;
+    let roomurl = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), vec![])?;
 
     let mut file = File::open(&avatar)?;
     let mut contents: Vec<u8> = vec![];
@@ -504,7 +513,7 @@ pub fn new_room(
     privacy: RoomType,
     internal_id: String,
 ) -> Result<(), Error> {
-    let url = bk.url("createRoom", &[])?;
+    let url = bk.url("createRoom", vec![])?;
     let attrs = json!({
         "invite": [],
         "invite_3pid": [],
@@ -539,7 +548,7 @@ pub fn new_room(
 }
 
 pub fn direct_chat(bk: &Backend, user: &Member, internal_id: String) -> Result<(), Error> {
-    let url = bk.url("createRoom", &[])?;
+    let url = bk.url("createRoom", vec![])?;
     let attrs = json!({
         "invite": [user.uid.clone()],
         "invite_3pid": [],
@@ -549,7 +558,7 @@ pub fn direct_chat(bk: &Backend, user: &Member, internal_id: String) -> Result<(
     });
 
     let userid = bk.data.lock().unwrap().user_id.clone();
-    let direct_url = bk.url(&format!("user/{}/account_data/m.direct", userid), &[])?;
+    let direct_url = bk.url(&format!("user/{}/account_data/m.direct", userid), vec![])?;
 
     let m = user.clone();
     let tx = bk.tx.clone();
@@ -587,7 +596,7 @@ pub fn add_to_fav(bk: &Backend, roomid: String, tofav: bool) -> Result<(), Error
     let userid = bk.data.lock().unwrap().user_id.clone();
     let url = bk.url(
         &format!("user/{}/rooms/{}/tags/m.favourite", userid, roomid),
-        &[],
+        vec![],
     )?;
 
     let attrs = json!({
@@ -613,7 +622,7 @@ pub fn add_to_fav(bk: &Backend, roomid: String, tofav: bool) -> Result<(), Error
 }
 
 pub fn invite(bk: &Backend, roomid: &str, userid: &str) -> Result<(), Error> {
-    let url = bk.url(&format!("rooms/{}/invite", roomid), &[])?;
+    let url = bk.url(&format!("rooms/{}/invite", roomid), vec![])?;
 
     let attrs = json!({
         "user_id": userid,
diff --git a/fractal-matrix-api/src/backend/stickers.rs b/fractal-matrix-api/src/backend/stickers.rs
index 2b0a54ad..0771753c 100644
--- a/fractal-matrix-api/src/backend/stickers.rs
+++ b/fractal-matrix-api/src/backend/stickers.rs
@@ -33,7 +33,7 @@ pub fn list(bk: &Backend) -> Result<(), Error> {
         ("widget_id", widget_id),
         ("filter_unpurchased", "true".to_string()),
     ];
-    let url = vurl(&bk.data, "widgets/assets", &data)?;
+    let url = vurl(&bk.data, "widgets/assets", data)?;
 
     let tx = bk.tx.clone();
     get!(
@@ -60,7 +60,7 @@ pub fn get_sticker_widget_id(bk: &Backend, then: BKCommand) -> Result<(), Error>
     let d = bk.data.clone();
     let itx = bk.internal_tx.clone();
 
-    let url = vurl(&d, "widgets/request", &[]).unwrap();
+    let url = vurl(&d, "widgets/request", vec![]).unwrap();
     post!(
         &url,
         &data,
@@ -107,7 +107,7 @@ pub fn send(bk: &Backend, roomid: &str, sticker: &Sticker) -> Result<(), Error>
     // TODO: we need to generate the msg.id in the frontend
     let id = format!("{:x}", digest);
 
-    let url = bk.url(&format!("rooms/{}/send/m.sticker/{}", roomid, id), &[])?;
+    let url = bk.url(&format!("rooms/{}/send/m.sticker/{}", roomid, id), vec![])?;
 
     let attrs = json!({
         "body": sticker.body.clone(),
@@ -151,7 +151,7 @@ pub fn purchase(bk: &Backend, group: &StickerGroup) -> Result<(), Error> {
         ("widget_id", widget_id.clone()),
         ("widget_type", "m.stickerpicker".to_string()),
     ];
-    let url = vurl(&bk.data, "widgets/purchase_asset", &data)?;
+    let url = vurl(&bk.data, "widgets/purchase_asset", data)?;
     let tx = bk.tx.clone();
     let itx = bk.internal_tx.clone();
     get!(
@@ -174,22 +174,21 @@ fn get_base_url(data: &Arc<Mutex<BackendData>>) -> Result<Url, Error> {
 fn url(
     data: &Arc<Mutex<BackendData>>,
     path: &str,
-    params: &[(&str, String)],
+    mut params: Vec<(&str, String)>,
 ) -> Result<Url, Error> {
     let base = get_base_url(data)?;
     let tk = data.lock().unwrap().access_token.clone();
 
-    let mut params2 = params.to_vec();
-    params2.push(("access_token", tk.clone()));
+    params.push(("access_token", tk.clone()));
 
-    client_url(&base, path, &params2)
+    client_url(&base, path, &params)
 }
 
 fn get_scalar_token(data: &Arc<Mutex<BackendData>>) -> Result<String, Error> {
     let s = data.lock().unwrap().scalar_url.clone();
     let uid = data.lock().unwrap().user_id.clone();
 
-    let url = url(data, &format!("user/{}/openid/request_token", uid), &[])?;
+    let url = url(data, &format!("user/{}/openid/request_token", uid), vec![])?;
     let js = json_q("post", &url, &json!({}), globals::TIMEOUT)?;
 
     let vurl = Url::parse(&format!("{}/api/register", s))?;
@@ -207,7 +206,7 @@ fn get_scalar_token(data: &Arc<Mutex<BackendData>>) -> Result<String, Error> {
 fn vurl(
     data: &Arc<Mutex<BackendData>>,
     path: &str,
-    params: &[(&str, String)],
+    mut params: Vec<(&str, String)>,
 ) -> Result<Url, Error> {
     let s = data.lock().unwrap().scalar_url.clone();
     let base = Url::parse(&s)?;
@@ -217,8 +216,7 @@ fn vurl(
         Some(t) => t.clone(),
     };
 
-    let mut params2 = params.to_vec();
-    params2.push(("scalar_token", tk));
+    params.push(("scalar_token", tk));
 
-    scalar_url(&base, path, &params2)
+    scalar_url(&base, path, &params)
 }
diff --git a/fractal-matrix-api/src/backend/sync.rs b/fractal-matrix-api/src/backend/sync.rs
index 6c3b0c22..5c01492c 100644
--- a/fractal-matrix-api/src/backend/sync.rs
+++ b/fractal-matrix-api/src/backend/sync.rs
@@ -59,7 +59,7 @@ pub fn sync(bk: &Backend, new_since: Option<String>, initial: bool) -> Result<()
     }
 
     let baseu = bk.get_base_url()?;
-    let url = bk.url("sync", &params)?;
+    let url = bk.url("sync", params)?;
 
     let tx = bk.tx.clone();
     let data = bk.data.clone();
diff --git a/fractal-matrix-api/src/backend/user.rs b/fractal-matrix-api/src/backend/user.rs
index 7ecdf5bd..b1b0ea9c 100644
--- a/fractal-matrix-api/src/backend/user.rs
+++ b/fractal-matrix-api/src/backend/user.rs
@@ -27,7 +27,7 @@ use serde_json::Value as JsonValue;
 
 pub fn get_username(bk: &Backend) -> Result<(), Error> {
     let id = bk.data.lock().unwrap().user_id.clone();
-    let url = bk.url(&format!("profile/{}/displayname", encode_uid(&id)), &[])?;
+    let url = bk.url(&format!("profile/{}/displayname", encode_uid(&id)), vec![])?;
     let tx = bk.tx.clone();
     get!(
         &url,
@@ -43,7 +43,7 @@ pub fn get_username(bk: &Backend) -> Result<(), Error> {
 
 pub fn set_username(bk: &Backend, name: String) -> Result<(), Error> {
     let id = bk.data.lock().unwrap().user_id.clone();
-    let url = bk.url(&format!("profile/{}/displayname", encode_uid(&id)), &[])?;
+    let url = bk.url(&format!("profile/{}/displayname", encode_uid(&id)), vec![])?;
 
     let attrs = json!({
         "displayname": name,
@@ -66,7 +66,7 @@ pub fn set_username(bk: &Backend, name: String) -> Result<(), Error> {
 }
 
 pub fn get_threepid(bk: &Backend) -> Result<(), Error> {
-    let url = bk.url(&format!("account/3pid"), &[])?;
+    let url = bk.url(&format!("account/3pid"), vec![])?;
     let tx = bk.tx.clone();
     get!(
         &url,
@@ -113,7 +113,7 @@ pub fn get_email_token(
     email: &str,
     client_secret: String,
 ) -> Result<(), Error> {
-    let url = bk.url("account/3pid/email/requestToken", &[])?;
+    let url = bk.url("account/3pid/email/requestToken", vec![])?;
 
     let attrs = json!({
         "id_server": identity[8..],
@@ -152,7 +152,7 @@ pub fn get_phone_token(
     phone: &str,
     client_secret: String,
 ) -> Result<(), Error> {
-    let url = bk.url(&format!("account/3pid/msisdn/requestToken"), &[])?;
+    let url = bk.url(&format!("account/3pid/msisdn/requestToken"), vec![])?;
 
     let attrs = json!({
         "id_server": identity[8..],
@@ -192,7 +192,7 @@ pub fn add_threepid(
     client_secret: &str,
     sid: String,
 ) -> Result<(), Error> {
-    let url = bk.url(&format!("account/3pid"), &[])?;
+    let url = bk.url(&format!("account/3pid"), vec![])?;
     let attrs = json!({
         "three_pid_creds": {
             "id_server": identity[8..],
@@ -285,7 +285,7 @@ pub fn change_password(
     old_password: &str,
     new_password: &str,
 ) -> Result<(), Error> {
-    let url = bk.url(&format!("account/password"), &[])?;
+    let url = bk.url(&format!("account/password"), vec![])?;
 
     let attrs = json!({
         "new_password": new_password,
@@ -318,7 +318,7 @@ pub fn account_destruction(
     password: &str,
     flag: bool,
 ) -> Result<(), Error> {
-    let url = bk.url(&format!("account/deactivate"), &[])?;
+    let url = bk.url(&format!("account/deactivate"), vec![])?;
 
     let attrs = json!({
         "erase": flag,
@@ -411,7 +411,7 @@ pub fn get_user_info_async(
 }
 
 pub fn get_username_async(bk: &Backend, uid: String, tx: Sender<String>) -> Result<(), Error> {
-    let url = bk.url(&format!("profile/{}/displayname", encode_uid(&uid)), &[])?;
+    let url = bk.url(&format!("profile/{}/displayname", encode_uid(&uid)), vec![])?;
     get!(
         &url,
         |r: JsonValue| {
@@ -463,7 +463,7 @@ pub fn set_user_avatar(bk: &Backend, avatar: String) -> Result<(), Error> {
     let tk = bk.data.lock().unwrap().access_token.clone();
     let params = vec![("access_token", tk.clone())];
     let mediaurl = media_url(&baseu, "upload", &params)?;
-    let url = bk.url(&format!("profile/{}/avatar_url", encode_uid(&id)), &[])?;
+    let url = bk.url(&format!("profile/{}/avatar_url", encode_uid(&id)), vec![])?;
 
     let mut file = File::open(&avatar)?;
     let mut contents: Vec<u8> = vec![];
@@ -493,7 +493,7 @@ pub fn set_user_avatar(bk: &Backend, avatar: String) -> Result<(), Error> {
 }
 
 pub fn search(bk: &Backend, term: &str) -> Result<(), Error> {
-    let url = bk.url(&format!("user_directory/search"), &[])?;
+    let url = bk.url(&format!("user_directory/search"), vec![])?;
 
     let attrs = json!({
         "search_term": term,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]