diff options
| -rw-r--r-- | src/http/mod.rs | 30 | ||||
| -rw-r--r-- | src/model/channel/channel_id.rs | 6 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 4 | ||||
| -rw-r--r-- | src/model/channel/guild_channel.rs | 4 | ||||
| -rw-r--r-- | src/model/channel/mod.rs | 4 | ||||
| -rw-r--r-- | src/model/channel/private_channel.rs | 4 |
6 files changed, 27 insertions, 25 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs index 51bc153..a2575a0 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -1428,8 +1428,8 @@ pub fn send_file<R: Read>(channel_id: u64, mut file: R, filename: &str, map: Jso /// if the file is too large to send. /// /// [`HttpError::InvalidRequest`]: enum.HttpError.html#variant.InvalidRequest -pub fn send_files<T: Into<AttachmentType>>(channel_id: u64, files: Vec<T>, map: JsonMap) - -> Result<Message> { +pub fn send_files<'a, T>(channel_id: u64, files: Vec<T>, map: JsonMap) + -> Result<Message> where T: Into<AttachmentType<'a>> { let uri = format!(api!("/channels/{}/messages"), channel_id); let url = match Url::parse(&uri) { Ok(url) => url, @@ -1605,29 +1605,31 @@ fn verify(expected_status_code: u16, mut response: HyperResponse) -> Result<()> Err(Error::Http(HttpError::InvalidRequest(response.status))) } +use std::path::Path; + /// Enum that allows a user to pass a `Path` or a `File` type to `send_files` -pub enum AttachmentType { +pub enum AttachmentType<'a> { /// Indicates that the `AttachmentType` is a `File` - File((File, String)), + File((&'a File, &'a str)), /// Indicates that the `AttachmentType` is a `Path` - Path(PathBuf), + Path(&'a Path), } -impl From<String> for AttachmentType { - fn from(s: String) -> AttachmentType { - AttachmentType::Path(PathBuf::from(&s)) +impl<'a> From<&'a str> for AttachmentType<'a> { + fn from(s: &'a str) -> AttachmentType { + AttachmentType::Path(Path::new(s)) } } -impl<'a> From<&'a str> for AttachmentType { - fn from(s: &'a str) -> AttachmentType { - AttachmentType::Path(PathBuf::from(s)) +impl<'a> From<&'a PathBuf> for AttachmentType<'a> { + fn from(pathbuf: &'a PathBuf) -> AttachmentType { + AttachmentType::Path(pathbuf.as_path()) } } -impl<'a> From<(File, &'a str)> for AttachmentType { - fn from(f: (File, &str)) -> AttachmentType { - AttachmentType::File((f.0, f.1.to_owned())) +impl<'a> From<(&'a File, &'a str)> for AttachmentType<'a> { + fn from(f: (&'a File, &'a str)) -> AttachmentType<'a> { + AttachmentType::File((f.0, f.1)) } } diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index de907b0..bb9932d 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -453,7 +453,7 @@ impl ChannelId { /// let f1 = File::open("my_file.jpg").unwrap(); /// let f2 = File::open("my_file2.jpg").unwrap(); /// - /// let files = vec![(f1, "my_file.jpg"), (f2, "my_file2.jpg")]; + /// let files = vec![(&f1, "my_file.jpg"), (&f2, "my_file2.jpg")]; /// /// let _ = channel_id.send_files(files, |m| m.content("a file")); /// ``` @@ -474,8 +474,8 @@ impl ChannelId { /// [`GuildChannel`]: struct.GuildChannel.html /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html - pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage { + pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { let mut map = f(CreateMessage::default()).0; if let Some(content) = map.get("content") { diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index ca4d48f..c1bc32a 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -327,8 +327,8 @@ impl Group { /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] - pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage { + pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { self.channel_id.send_files(files, f) } diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index f51276c..a6e90e8 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -587,8 +587,8 @@ impl GuildChannel { /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] - pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage { + pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { self.id.send_files(files, f) } diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 269b702..aed04ab 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -323,8 +323,8 @@ impl Channel { /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] - pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage { + pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { self.id().send_files(files, f) } diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs index ba90185..11c99ac 100644 --- a/src/model/channel/private_channel.rs +++ b/src/model/channel/private_channel.rs @@ -273,8 +273,8 @@ impl PrivateChannel { /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[inline] - pub fn send_files<F, T: Into<AttachmentType>>(&self, files: Vec<T>, f: F) -> Result<Message> - where F: FnOnce(CreateMessage) -> CreateMessage { + pub fn send_files<'a, F, T>(&self, files: Vec<T>, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage, T: Into<AttachmentType<'a>> { self.id.send_files(files, f) } |