Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove useless allocations #37

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ octocrab = "0.19.0"
reqwest = "0.11.22"
hex = "0.4.3"
to-arraystring = "0.1.3"
arrayvec = "0.7.4"
58 changes: 43 additions & 15 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub(crate) const ACCENT_COLOUR: Colour = Colour(0x8957e5);
pub(crate) const OK_COLOUR: Colour = Colour(0x2ecc71);
pub(crate) const ERROR_COLOUR: Colour = Colour(0xe74c3c);

use arrayvec::ArrayString;
use to_arraystring::ToArrayString;

use crate::{Context, Error};

use poise::serenity_prelude::{
Expand Down Expand Up @@ -64,20 +67,43 @@ pub async fn interaction_err(ctx: &serenity::Context, press: &ComponentInteracti
let _ = press.create_response(ctx, builder).await;
}

pub async fn paginate_lists<U, E>(
ctx: poise::Context<'_, U, E>,
enum Kind {
Next,
Previous,
}

impl Kind {
fn from_id(id: &str, ctx_id: &str) -> Option<Self> {
let this = match id.strip_prefix(ctx_id)? {
"next" => Self::Next,
"prev" => Self::Previous,
_ => return None,
};

Some(this)
}
}

pub async fn paginate_lists(
ctx: crate::Context<'_>,
pages: &[Vec<(String, String, bool)>],
embed_title: &str,
) -> Result<(), Error> {
let ctx_id = ctx.id();
let prev_button_id = format!("{ctx_id}prev");
let next_button_id = format!("{ctx_id}next");
let ctx_id = ctx.id().to_arraystring();

let mut prev_button_id = ArrayString::<24>::new();
prev_button_id.push_str(&ctx_id);
prev_button_id.push_str("prev");

let mut next_button_id = ArrayString::<24>::new();
next_button_id.push_str(&ctx_id);
next_button_id.push_str("next");

let colour = Colour::TEAL;

let components = CreateActionRow::Buttons(vec![
CreateButton::new(&prev_button_id).emoji('◀'),
CreateButton::new(&next_button_id).emoji('▶'),
CreateButton::new(&*prev_button_id).emoji('◀'),
CreateButton::new(&*next_button_id).emoji('▶'),
]);
let mut current_page = 0;

Expand Down Expand Up @@ -113,15 +139,17 @@ pub async fn paginate_lists<U, E>(
.timeout(std::time::Duration::from_secs(180))
.await
{
if press.data.custom_id == next_button_id {
current_page += 1;
if current_page >= pages.len() {
current_page = 0;
match Kind::from_id(&press.data.custom_id, &ctx_id) {
Some(Kind::Next) => {
current_page += 1;
if current_page >= pages.len() {
current_page = 0;
}
}
Some(Kind::Previous) => {
current_page = current_page.checked_sub(1).unwrap_or(pages.len() - 1);
}
} else if press.data.custom_id == prev_button_id {
current_page = current_page.checked_sub(1).unwrap_or(pages.len() - 1);
} else {
continue;
None => continue,
}

press
Expand Down
19 changes: 14 additions & 5 deletions src/events/issues/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::time::Duration;

use crate::{commands::interaction_err, structures::Embeddable, Data};

use arrayvec::ArrayString;
use poise::serenity_prelude::{
self as serenity, ButtonStyle, Context, CreateActionRow, CreateButton, CreateEmbed,
CreateInteractionResponse, Message, Permissions,
Expand Down Expand Up @@ -43,18 +44,26 @@ pub async fn message(data: &Data, ctx: &Context, message: &Message) {
// we can avoid even a stack allocation! (thanks gnome)
let ctx_id = message.id.get().to_arraystring();

let remove_id = format!("{ctx_id}delete");
let hide_body_id = format!("{ctx_id}hide_body");
// we know the max size so we don't need to allocate.
// 20 + 6
let mut remove_id = ArrayString::<26>::new();
remove_id.push_str(&ctx_id);
remove_id.push_str("delete");

let remove = CreateActionRow::Buttons(vec![CreateButton::new(&remove_id)
// 20 + 9
let mut hide_body_id = ArrayString::<29>::new();
hide_body_id.push_str(&ctx_id);
hide_body_id.push_str("hide_body");

let remove = CreateActionRow::Buttons(vec![CreateButton::new(&*remove_id)
.label("delete")
.style(ButtonStyle::Danger)]);

let components = serenity::CreateActionRow::Buttons(vec![
CreateButton::new(&remove_id)
CreateButton::new(&*remove_id)
.label("delete")
.style(ButtonStyle::Danger),
CreateButton::new(&hide_body_id).label("hide body"),
CreateButton::new(&*hide_body_id).label("hide body"),
]);

let content: serenity::CreateMessage = serenity::CreateMessage::default()
Expand Down
9 changes: 5 additions & 4 deletions src/events/issues/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use octocrab::models::{issues::Issue, pulls::PullRequest};
use poise::serenity_prelude::{Colour, CreateEmbed, CreateEmbedAuthor};

use crate::structures::Embeddable;
use std::fmt::Write;

const OPEN_COLOUR: Colour = Colour::new(0x238636);
const RESOLVED_COLOUR: Colour = Colour::new(0x8957e5);
Expand Down Expand Up @@ -47,9 +48,9 @@ impl Document for Issue {
fn get_content(&self) -> String {
let body = self.body.as_deref().unwrap_or_default();

let mut description = String::default();
let mut description = String::new();
for line in body.split('\n').take(15) {
description.push_str(&format!("{line}\n"));
writeln!(description, "{line}").unwrap();
}

description.shrink_to(4096);
Expand Down Expand Up @@ -123,9 +124,9 @@ impl Document for PullRequest {
fn get_content(&self) -> String {
let body = self.body.as_deref().unwrap_or_default();

let mut content = String::default();
let mut content = String::new();
for line in body.split('\n').take(15) {
content.push_str(&format!("{line}\n"));
writeln!(content, "{line}").unwrap();
}

content.shrink_to(4096);
Expand Down