Skip to content

Commit 5ed711c

Browse files
author
Kharif
committed
String -> Arc<str>
1 parent eb6b9d5 commit 5ed711c

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

examples/flip_cards/src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rust_search::SearchBuilder;
33
use serde_json::{Map, Value};
44
use std::collections::{HashSet, VecDeque};
55
use std::fs;
6+
use std::sync::Arc;
67

78
// Function to find and extract flip cards from JSON data
89
fn find_flip_cards(value: &Value) -> Vec<Value> {
@@ -74,8 +75,8 @@ fn find_flip_cards(value: &Value) -> Vec<Value> {
7475
#[derive(Clone)]
7576
struct FlipCardState {
7677
show_answer: bool,
77-
question: String,
78-
answer: String,
78+
question: Arc<str>,
79+
answer: Arc<str>,
7980
}
8081

8182
// Struct to represent the state of all flip cards
@@ -114,8 +115,8 @@ fn main() {
114115
.iter()
115116
.map(|card| FlipCardState {
116117
show_answer: false,
117-
question: card["q"].as_str().unwrap().to_string(),
118-
answer: card["a"].as_str().unwrap().to_string(),
118+
question: card["q"].to_string().into(),
119+
answer: card["a"].to_string().into(),
119120
})
120121
.collect(),
121122
current_index: 0, // Start at the first card
@@ -128,9 +129,9 @@ fn main() {
128129
// Render the current flip card
129130
vstack((
130131
text(if current_card.show_answer {
131-
current_card.answer.as_str()
132+
&current_card.answer
132133
} else {
133-
current_card.question.as_str()
134+
&current_card.question
134135
})
135136
.font_size(12)
136137
.padding(Auto),

src/views/text.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ macro_rules! impl_text {
106106
let txt = &format!("{}", self);
107107
let vger = &mut args.vger;
108108
let origin = vger.text_bounds(txt, Text::DEFAULT_SIZE, None).origin;
109-
109+
110110
vger.save();
111111
vger.translate([-origin.x, -origin.y]);
112112
vger.text(txt, Text::DEFAULT_SIZE, TEXT_COLOR, None);
@@ -116,7 +116,7 @@ macro_rules! impl_text {
116116
let txt = &format!("{}", self);
117117
(args.text_bounds)(txt, Text::DEFAULT_SIZE, None).size
118118
}
119-
119+
120120
fn access(
121121
&self,
122122
path: &mut IdPath,
@@ -131,8 +131,7 @@ macro_rules! impl_text {
131131
}
132132
}
133133

134-
impl TextModifiers for $ty
135-
{
134+
impl TextModifiers for $ty {
136135
fn font_size(self, size: u32) -> Text {
137136
Text {
138137
text: format!("{}", self),
@@ -159,8 +158,7 @@ macro_rules! impl_text {
159158
}
160159
}
161160
}
162-
163-
}
161+
};
164162
}
165163

166164
// XXX: this used to be generic for any Display but
@@ -205,8 +203,7 @@ impl DynView for &'static str {
205203
}
206204
}
207205

208-
impl TextModifiers for &'static str
209-
{
206+
impl TextModifiers for &'static str {
210207
fn font_size(self, size: u32) -> Text {
211208
Text {
212209
text: format!("{}", self),

0 commit comments

Comments
 (0)