summaryrefslogtreecommitdiffstats
path: root/examples/websocket
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-02 09:29:16 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-04-02 09:29:16 +0200
commit488cac714896002791f3c7b9a99181310c1d1b5c (patch)
tree46d3f1b92efcc3b815662c23aed292dae722366d /examples/websocket
parentdee43d5f66c97dddffebbc02c4a87674fb2c13b9 (diff)
downloadiced-488cac714896002791f3c7b9a99181310c1d1b5c.tar.gz
iced-488cac714896002791f3c7b9a99181310c1d1b5c.tar.bz2
iced-488cac714896002791f3c7b9a99181310c1d1b5c.zip
Avoid extra text allocations in `websocket` example
Diffstat (limited to 'examples/websocket')
-rw-r--r--examples/websocket/src/echo.rs23
-rw-r--r--examples/websocket/src/main.rs4
2 files changed, 18 insertions, 9 deletions
diff --git a/examples/websocket/src/echo.rs b/examples/websocket/src/echo.rs
index 281ed4bd..cd32cb66 100644
--- a/examples/websocket/src/echo.rs
+++ b/examples/websocket/src/echo.rs
@@ -2,6 +2,7 @@ pub mod server;
use iced::futures;
use iced::subscription::{self, Subscription};
+use iced::widget::text;
use futures::channel::mpsc;
use futures::sink::SinkExt;
@@ -136,16 +137,24 @@ impl Message {
pub fn disconnected() -> Self {
Message::Disconnected
}
+
+ pub fn as_str(&self) -> &str {
+ match self {
+ Message::Connected => "Connected successfully!",
+ Message::Disconnected => "Connection lost... Retrying...",
+ Message::User(message) => message.as_str(),
+ }
+ }
}
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- Message::Connected => write!(f, "Connected successfully!"),
- Message::Disconnected => {
- write!(f, "Connection lost... Retrying...")
- }
- Message::User(message) => write!(f, "{message}"),
- }
+ f.write_str(self.as_str())
+ }
+}
+
+impl<'a> text::IntoFragment<'a> for &'a Message {
+ fn into_fragment(self) -> text::Fragment<'a> {
+ text::Fragment::Borrowed(self.as_str())
}
}
diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs
index 369ae820..b479fe89 100644
--- a/examples/websocket/src/main.rs
+++ b/examples/websocket/src/main.rs
@@ -2,7 +2,7 @@ mod echo;
use iced::alignment::{self, Alignment};
use iced::widget::{
- button, column, container, row, scrollable, text, text_input, value,
+ button, column, container, row, scrollable, text, text_input,
};
use iced::{color, Command, Element, Length, Subscription};
use once_cell::sync::Lazy;
@@ -96,7 +96,7 @@ impl WebSocket {
.into()
} else {
scrollable(
- column(self.messages.iter().map(value).map(Element::from))
+ column(self.messages.iter().map(text).map(Element::from))
.spacing(10),
)
.id(MESSAGE_LOG.clone())