From 53bea1c8b77ff4a7a421dd7dd5fc10f04385429b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Sat, 21 Dec 2024 16:17:37 +0100 Subject: [PATCH] feat!(sdk): Accept any type that implements `Into` as an attachment filename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we need a String as final type, this can avoid to allocate a string if the provided type is already a String. Signed-off-by: Kévin Commaille --- crates/matrix-sdk/src/room/futures.rs | 4 ++-- crates/matrix-sdk/src/room/mod.rs | 12 ++++++------ crates/matrix-sdk/src/send_queue/upload.rs | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/matrix-sdk/src/room/futures.rs b/crates/matrix-sdk/src/room/futures.rs index 4c1878f870e..2aaf6c38b56 100644 --- a/crates/matrix-sdk/src/room/futures.rs +++ b/crates/matrix-sdk/src/room/futures.rs @@ -240,7 +240,7 @@ impl<'a> IntoFuture for SendRawMessageLikeEvent<'a> { #[allow(missing_debug_implementations)] pub struct SendAttachment<'a> { room: &'a Room, - filename: &'a str, + filename: String, content_type: &'a Mime, data: Vec, config: AttachmentConfig, @@ -252,7 +252,7 @@ pub struct SendAttachment<'a> { impl<'a> SendAttachment<'a> { pub(crate) fn new( room: &'a Room, - filename: &'a str, + filename: String, content_type: &'a Mime, data: Vec, config: AttachmentConfig, diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 50ea5341984..6ea0470e7a9 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -1933,12 +1933,12 @@ impl Room { #[instrument(skip_all)] pub fn send_attachment<'a>( &'a self, - filename: &'a str, + filename: impl Into, content_type: &'a Mime, data: Vec, config: AttachmentConfig, ) -> SendAttachment<'a> { - SendAttachment::new(self, filename, content_type, data, config) + SendAttachment::new(self, filename.into(), content_type, data, config) } /// Prepare and send an attachment to this room. @@ -1971,7 +1971,7 @@ impl Room { #[instrument(skip_all)] pub(super) async fn prepare_and_send_attachment<'a>( &'a self, - filename: &'a str, + filename: String, content_type: &'a Mime, data: Vec, mut config: AttachmentConfig, @@ -2076,7 +2076,7 @@ impl Room { pub(crate) fn make_attachment_type( &self, content_type: &Mime, - filename: &str, + filename: String, source: MediaSource, caption: Option, formatted_caption: Option, @@ -2087,8 +2087,8 @@ impl Room { // body is the filename, and the filename is not set. // https://github.com/matrix-org/matrix-spec-proposals/blob/main/proposals/2530-body-as-caption.md let (body, filename) = match caption { - Some(caption) => (caption, Some(filename.to_owned())), - None => (filename.to_owned(), None), + Some(caption) => (caption, Some(filename)), + None => (filename, None), }; let (thumbnail_source, thumbnail_info) = thumbnail.unzip(); diff --git a/crates/matrix-sdk/src/send_queue/upload.rs b/crates/matrix-sdk/src/send_queue/upload.rs index 7c32c8fa000..93a7aec7cb5 100644 --- a/crates/matrix-sdk/src/send_queue/upload.rs +++ b/crates/matrix-sdk/src/send_queue/upload.rs @@ -105,7 +105,7 @@ impl RoomSendQueue { #[instrument(skip_all, fields(event_txn))] pub async fn send_attachment( &self, - filename: &str, + filename: impl Into, content_type: Mime, data: Vec, mut config: AttachmentConfig, @@ -118,6 +118,7 @@ impl RoomSendQueue { return Err(RoomSendQueueError::RoomNotJoined); } + let filename = filename.into(); let upload_file_txn = TransactionId::new(); let send_event_txn = config.txn_id.map_or_else(ChildTransactionId::new, Into::into);