Skip to content

Commit

Permalink
Use Cow's in TextComponent
Browse files Browse the repository at this point in the history
We now don't always convert &str to Strings allowing us to use stack allocations
  • Loading branch information
Snowiiii committed Aug 19, 2024
1 parent dfc0b8a commit a6d9ed2
Show file tree
Hide file tree
Showing 26 changed files with 134 additions and 149 deletions.
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/bytebuf/packet_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{BitSet, ClientPacket, ServerPacket, VarInt, VarIntType};

use super::{deserializer, serializer, ByteBuffer, DeserializerError};

impl Serialize for BitSet<'static> {
impl<'a> Serialize for BitSet<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/config/c_add_resource_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct CConfigAddResourcePack<'a> {
url: &'a str,
hash: &'a str, // max 40
forced: bool,
prompt_message: Option<TextComponent>,
prompt_message: Option<TextComponent<'a>>,
}

impl<'a> CConfigAddResourcePack<'a> {
Expand All @@ -20,7 +20,7 @@ impl<'a> CConfigAddResourcePack<'a> {
url: &'a str,
hash: &'a str,
forced: bool,
prompt_message: Option<TextComponent>,
prompt_message: Option<TextComponent<'a>>,
) -> Self {
Self {
uuid,
Expand Down
1 change: 0 additions & 1 deletion pumpkin-protocol/src/client/config/c_plugin_message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket};

#[derive(Serialize)]
#[packet(0x01)]
Expand Down
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_actionbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use serde::Serialize;

#[derive(Serialize)]
#[packet(0x4C)]
pub struct CActionBar {
action_bar: TextComponent,
pub struct CActionBar<'a> {
action_bar: TextComponent<'a>,
}

impl CActionBar {
pub fn new(action_bar: TextComponent) -> Self {
impl<'a> CActionBar<'a> {
pub fn new(action_bar: TextComponent<'a>) -> Self {
Self { action_bar }
}
}
16 changes: 8 additions & 8 deletions pumpkin-protocol/src/client/play/c_disguised_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ use crate::VarInt;

#[derive(Serialize)]
#[packet(0x1E)]
pub struct CDisguisedChatMessage {
message: TextComponent,
pub struct CDisguisedChatMessage<'a> {
message: TextComponent<'a>,
chat_type: VarInt,
sender_name: TextComponent,
target_name: Option<TextComponent>,
sender_name: TextComponent<'a>,
target_name: Option<TextComponent<'a>>,
}

impl CDisguisedChatMessage {
impl<'a> CDisguisedChatMessage<'a> {
pub fn new(
message: TextComponent,
message: TextComponent<'a>,
chat_type: VarInt,
sender_name: TextComponent,
target_name: Option<TextComponent>,
sender_name: TextComponent<'a>,
target_name: Option<TextComponent<'a>>,
) -> Self {
Self {
message,
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/play/c_login.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::VarInt;
use crate::{position::WorldPosition, VarInt};

#[derive(Serialize)]
#[packet(0x2B)]
Expand All @@ -23,7 +23,7 @@ pub struct CLogin<'a> {
previous_gamemode: i8,
debug: bool,
is_flat: bool,
death_dimension_name: Option<(String, i64)>, // POSITION NOT STRING
death_dimension_name: Option<(WorldPosition, i64)>,
portal_cooldown: VarInt,
enforce_secure_chat: bool,
}
Expand All @@ -47,7 +47,7 @@ impl<'a> CLogin<'a> {
previous_gamemode: i8,
debug: bool,
is_flat: bool,
death_dimension_name: Option<(String, i64)>,
death_dimension_name: Option<(WorldPosition, i64)>,
portal_cooldown: VarInt,
enforce_secure_chat: bool,
) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_open_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::VarInt;

#[derive(Serialize)]
#[packet(0x33)]
pub struct COpenScreen {
pub struct COpenScreen<'a> {
window_id: VarInt,
window_type: VarInt,
window_title: TextComponent,
window_title: TextComponent<'a>,
}

impl COpenScreen {
pub fn new(window_id: VarInt, window_type: VarInt, window_title: TextComponent) -> Self {
impl<'a> COpenScreen<'a> {
pub fn new(window_id: VarInt, window_type: VarInt, window_title: TextComponent<'a>) -> Self {
Self {
window_id,
window_type,
Expand Down
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_play_disconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use serde::Serialize;

#[derive(Serialize)]
#[packet(0x1D)]
pub struct CPlayDisconnect {
reason: TextComponent,
pub struct CPlayDisconnect<'a> {
reason: TextComponent<'a>,
}

impl CPlayDisconnect {
pub fn new(reason: TextComponent) -> Self {
impl<'a> CPlayDisconnect<'a> {
pub fn new(reason: TextComponent<'a>) -> Self {
Self { reason }
}
}
22 changes: 11 additions & 11 deletions pumpkin-protocol/src/client/play/c_player_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ pub struct CPlayerChatMessage<'a> {
sender: UUID,
index: VarInt,
message_signature: Option<&'a [u8]>,
message: String,
message: &'a str,
timestamp: i64,
salt: i64,
previous_messages_count: VarInt,
previous_messages: &'a [PreviousMessage<'a>], // max 20
unsigned_content: Option<TextComponent>,
unsigned_content: Option<TextComponent<'a>>,
/// See `FilterType`
filter_type: VarInt,
// TODO: THIS IS A HACK, We currently don't support writing or reading bitsets
filter_type_bits: bool,
filter_type_bits: Option<BitSet<'a>>,
chat_type: VarInt,
sender_name: TextComponent,
target_name: Option<TextComponent>,
sender_name: TextComponent<'a>,
target_name: Option<TextComponent<'a>>,
}

impl<'a> CPlayerChatMessage<'a> {
Expand All @@ -32,15 +31,16 @@ impl<'a> CPlayerChatMessage<'a> {
sender: UUID,
index: VarInt,
message_signature: Option<&'a [u8]>,
message: String,
message: &'a str,
timestamp: i64,
salt: i64,
previous_messages: &'a [PreviousMessage<'a>],
unsigned_content: Option<TextComponent>,
unsigned_content: Option<TextComponent<'a>>,
filter_type: VarInt,
filter_type_bits: Option<BitSet<'a>>,
chat_type: VarInt,
sender_name: TextComponent,
target_name: Option<TextComponent>,
sender_name: TextComponent<'a>,
target_name: Option<TextComponent<'a>>,
) -> Self {
Self {
sender,
Expand All @@ -53,7 +53,7 @@ impl<'a> CPlayerChatMessage<'a> {
previous_messages,
unsigned_content,
filter_type,
filter_type_bits: false,
filter_type_bits,
chat_type,
sender_name,
target_name,
Expand Down
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_set_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use serde::Serialize;

#[derive(Serialize)]
#[packet(0x65)]
pub struct CTitleText {
title: TextComponent,
pub struct CTitleText<'a> {
title: TextComponent<'a>,
}

impl CTitleText {
pub fn new(title: TextComponent) -> Self {
impl<'a> CTitleText<'a> {
pub fn new(title: TextComponent<'a>) -> Self {
Self { title }
}
}
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_subtitle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use serde::Serialize;

#[derive(Serialize)]
#[packet(0x63)]
pub struct CSubtitle {
subtitle: TextComponent,
pub struct CSubtitle<'a> {
subtitle: TextComponent<'a>,
}

impl CSubtitle {
pub fn new(subtitle: TextComponent) -> Self {
impl<'a> CSubtitle<'a> {
pub fn new(subtitle: TextComponent<'a>) -> Self {
Self { subtitle }
}
}
8 changes: 4 additions & 4 deletions pumpkin-protocol/src/client/play/c_system_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use serde::Serialize;

#[derive(Serialize)]
#[packet(0x6C)]
pub struct CSystemChatMessge {
content: TextComponent,
pub struct CSystemChatMessge<'a> {
content: TextComponent<'a>,
overlay: bool,
}

impl CSystemChatMessge {
pub fn new(content: TextComponent, overlay: bool) -> Self {
impl<'a> CSystemChatMessge<'a> {
pub fn new(content: TextComponent<'a>, overlay: bool) -> Self {
Self { content, overlay }
}
}
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytebuf::{packet_id::Packet, ByteBuffer, DeserializerError};
use bytes::Buf;
use serde::{Deserialize, Serialize, Serializer};
use serde::{Deserialize, Serialize};
use std::io::{self, Write};
use thiserror::Error;

Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/server/play/s_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use pumpkin_macros::packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
ServerPacket,
ServerPacket, VarInt,
};

// derive(Deserialize)]
Expand All @@ -12,7 +12,7 @@ pub struct SChatMessage {
pub timestamp: i64,
pub salt: i64,
pub signature: Option<Vec<u8>>,
// pub messagee_count: VarInt,
pub messagee_count: VarInt,
// acknowledged: BitSet,
}

Expand All @@ -24,7 +24,7 @@ impl ServerPacket for SChatMessage {
timestamp: bytebuf.get_i64(),
salt: bytebuf.get_i64(),
signature: bytebuf.get_option(|v| v.get_slice().to_vec()),
//messagee_count: bytebuf.get_var_int(),
messagee_count: bytebuf.get_var_int(),
})
}
}
1 change: 0 additions & 1 deletion pumpkin-protocol/src/server/play/s_interact.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::char::MAX;

use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-registry/src/chat_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct ChatType {
pub struct Decoration {
translation_key: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
style: Option<Style>,
style: Option<Style<'static>>,
parameters: Vec<String>,
}

Expand Down
12 changes: 7 additions & 5 deletions pumpkin-text/src/click.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use std::borrow::Cow;

use serde::{Deserialize, Serialize};

/// Action to take on click of the text.
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
#[serde(tag = "action", content = "value", rename_all = "snake_case")]
pub enum ClickEvent {
pub enum ClickEvent<'a> {
/// Opens a URL
OpenUrl(String),
OpenUrl(Cow<'a, str>),
/// Works in signs, but only on the root text component
RunCommand(String),
RunCommand(Cow<'a, str>),
/// Replaces the contents of the chat box with the text, not necessarily a
/// command.
SuggestCommand(String),
SuggestCommand(Cow<'a, str>),
/// Only usable within written books. Changes the page of the book. Indexing
/// starts at 1.
ChangePage(i32),
/// Copies the given text to system clipboard
CopyToClipboard(String),
CopyToClipboard(Cow<'a, str>),
}
14 changes: 8 additions & 6 deletions pumpkin-text/src/hover.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use std::borrow::Cow;

use serde::{Deserialize, Serialize};

use crate::Text;

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "action", content = "contents", rename_all = "snake_case")]
#[allow(clippy::enum_variant_names)]
pub enum HoverEvent {
pub enum HoverEvent<'a> {
/// Displays a tooltip with the given text.
ShowText(Text),
ShowText(Text<'a>),
/// Shows an item.
ShowItem {
/// Resource identifier of the item
id: String,
id: Cow<'a, str>,
/// Number of the items in the stack
count: Option<i32>,
/// NBT information about the item (sNBT format)
tag: String,
tag: Cow<'a, str>,
},
/// Shows an entity.
ShowEntity {
Expand All @@ -24,9 +26,9 @@ pub enum HoverEvent {
/// Resource identifier of the entity
#[serde(rename = "type")]
#[serde(default, skip_serializing_if = "Option::is_none")]
kind: Option<String>,
kind: Option<Cow<'a, str>>,
/// Optional custom name for the entity
#[serde(default, skip_serializing_if = "Option::is_none")]
name: Option<Text>,
name: Option<Text<'a>>,
},
}
Loading

0 comments on commit a6d9ed2

Please sign in to comment.