Skip to content

Commit

Permalink
Backport: Fix model functions using SerializeIter (serenity-rs#3006)
Browse files Browse the repository at this point in the history
This is a backport of serenity-rs#2955 to `current`. `bulk_ban` is currently uncallable
so this "breaking" change doesn't actually break anyone.
  • Loading branch information
GnomedDev authored Oct 28, 2024
1 parent 2fb922e commit 59c2450
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 30 deletions.
9 changes: 4 additions & 5 deletions src/model/guild/guild_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use crate::http::{CacheHttp, Http, UserPagination};
use crate::internal::prelude::*;
#[cfg(feature = "model")]
use crate::json::json;
use crate::model::guild::SerializeIter;
use crate::model::prelude::*;

#[cfg(feature = "model")]
Expand Down Expand Up @@ -263,18 +262,18 @@ impl GuildId {
pub async fn bulk_ban(
self,
http: &Http,
users: impl IntoIterator<Item = UserId>,
user_ids: &[UserId],
delete_message_seconds: u32,
reason: Option<&str>,
) -> Result<BulkBanResponse> {
#[derive(serde::Serialize)]
struct BulkBan<I> {
user_ids: I,
struct BulkBan<'a> {
user_ids: &'a [UserId],
delete_message_seconds: u32,
}

let map = BulkBan {
user_ids: SerializeIter::new(users.into_iter()),
user_ids,
delete_message_seconds,
};

Expand Down
4 changes: 2 additions & 2 deletions src/model/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl Guild {
pub async fn bulk_ban(
&self,
cache_http: impl CacheHttp,
users: impl IntoIterator<Item = UserId>,
user_ids: &[UserId],
delete_message_seconds: u32,
reason: Option<&str>,
) -> Result<BulkBanResponse> {
Expand All @@ -545,7 +545,7 @@ impl Guild {
}
}

self.id.bulk_ban(cache_http.http(), users, delete_message_seconds, reason).await
self.id.bulk_ban(cache_http.http(), user_ids, delete_message_seconds, reason).await
}

/// Returns the formatted URL of the guild's banner image, if one exists.
Expand Down
23 changes: 0 additions & 23 deletions src/model/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cell::Cell;
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
Expand Down Expand Up @@ -85,28 +84,6 @@ where
}
}

pub(super) struct SerializeIter<I>(Cell<Option<I>>);

impl<I> SerializeIter<I> {
pub fn new(iter: I) -> Self {
Self(Cell::new(Some(iter)))
}
}

impl<Iter, Item> serde::Serialize for SerializeIter<Iter>
where
Iter: Iterator<Item = Item>,
Item: serde::Serialize,
{
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let Some(iter) = self.0.take() else {
return serializer.serialize_seq(Some(0))?.end();
};

serializer.collect_seq(iter)
}
}

pub(super) enum StrOrInt<'de> {
String(String),
Str(&'de str),
Expand Down

0 comments on commit 59c2450

Please sign in to comment.