Skip to content

Commit

Permalink
Use an as_ref hack
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis authored and Zeyla Hellyer committed Oct 9, 2017
1 parent 934eb3a commit e0e7617
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/builder/edit_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ impl EditMember {
/// Requires the [Manage Roles] permission to modify.
///
/// [Manage Roles]: ../model/permissions/constant.MANAGE_ROLES.html
pub fn roles<'a, It: IntoIterator<Item=&'a RoleId>>(mut self, roles: It) -> Self {
pub fn roles<T: AsRef<RoleId>, It: IntoIterator<Item=T>>(mut self, roles: It) -> Self {
let role_ids = roles
.into_iter()
.map(|x| Value::Number(Number::from(x.0)))
.map(|x| Value::Number(Number::from(x.as_ref().0)))
.collect();

self.0.insert("roles".to_string(), Value::Array(role_ids));
Expand Down
6 changes: 3 additions & 3 deletions src/gateway/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,14 @@ impl Shard {
/// ../../model/event/enum.Event.html#variant.GuildMembersChunk
/// [`Guild`]: ../../model/struct.Guild.html
/// [`Member`]: ../../model/struct.Member.html
pub fn chunk_guilds<'a, It>(&mut self, guild_ids: It, limit: Option<u16>, query: Option<&str>)
where It: IntoIterator<Item=&'a GuildId>{
pub fn chunk_guilds<T: AsRef<GuildId>, It>(&mut self, guild_ids: It, limit: Option<u16>, query: Option<&str>)
where It: IntoIterator<Item=T> {
debug!("[Shard {:?}] Requesting member chunks", self.shard_info);

let msg = json!({
"op": OpCode::GetGuildMembers.num(),
"d": {
"guild_id": guild_ids.into_iter().map(|x| x.0).collect::<Vec<u64>>(),
"guild_id": guild_ids.into_iter().map(|x| x.as_ref().0).collect::<Vec<u64>>(),
"limit": limit.unwrap_or(0),
"query": query.unwrap_or(""),
},
Expand Down
4 changes: 2 additions & 2 deletions src/model/channel/channel_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ impl ChannelId {
///
/// [`Channel::delete_messages`]: enum.Channel.html#method.delete_messages
/// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html
pub fn delete_messages<'a, It: IntoIterator<Item=&'a MessageId>>(&self, message_ids: It) -> Result<()> {
pub fn delete_messages<T: AsRef<MessageId>, It: IntoIterator<Item=T>>(&self, message_ids: It) -> Result<()> {
let ids = message_ids
.into_iter()
.map(|message_id| message_id.0)
.map(|message_id| message_id.as_ref().0)
.collect::<Vec<u64>>();

let map = json!({ "messages": ids });
Expand Down
2 changes: 1 addition & 1 deletion src/model/channel/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Group {
/// [`Channel::delete_messages`]: enum.Channel.html#method.delete_messages
/// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html
#[inline]
pub fn delete_messages<'a, It: IntoIterator<Item=&'a MessageId>>(&self, message_ids: It) -> Result<()> {
pub fn delete_messages<T: AsRef<MessageId>, It: IntoIterator<Item=T>>(&self, message_ids: It) -> Result<()> {
self.channel_id.delete_messages(message_ids)
}

Expand Down
7 changes: 7 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ macro_rules! id_u64 {
}
}

// This is a hack so that functions can accept `IntoIterator<Item=IdType>`, or `IntoIterator<Item=&IdType>`
impl AsRef<$name> for $name {
fn as_ref(&self) -> &Self {
self
}
}

impl From<u64> for $name {
fn from(id_as_u64: u64) -> $name {
$name(id_as_u64)
Expand Down
4 changes: 2 additions & 2 deletions src/voice/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ fn encryption_key(client: &mut Client) -> Result<Key> {
}

#[inline]
fn has_valid_mode<'a, T, It> (modes: It) -> bool
where T: PartialEq<&'a str>,
fn has_valid_mode<T, It> (modes: It) -> bool
where T: PartialEq<str>,
It : IntoIterator<Item=T>
{
modes.into_iter().any(|s| s == CRYPTO_MODE)
Expand Down

0 comments on commit e0e7617

Please sign in to comment.