From 5ed711cc48db3fb998be0218db21637d1ab10631 Mon Sep 17 00:00:00 2001 From: Kharif Date: Fri, 31 Jan 2025 01:44:52 +0100 Subject: [PATCH] String -> Arc --- examples/flip_cards/src/main.rs | 13 +++++++------ src/views/text.rs | 13 +++++-------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/examples/flip_cards/src/main.rs b/examples/flip_cards/src/main.rs index 930f0da..e5e93db 100644 --- a/examples/flip_cards/src/main.rs +++ b/examples/flip_cards/src/main.rs @@ -3,6 +3,7 @@ use rust_search::SearchBuilder; use serde_json::{Map, Value}; use std::collections::{HashSet, VecDeque}; use std::fs; +use std::sync::Arc; // Function to find and extract flip cards from JSON data fn find_flip_cards(value: &Value) -> Vec { @@ -74,8 +75,8 @@ fn find_flip_cards(value: &Value) -> Vec { #[derive(Clone)] struct FlipCardState { show_answer: bool, - question: String, - answer: String, + question: Arc, + answer: Arc, } // Struct to represent the state of all flip cards @@ -114,8 +115,8 @@ fn main() { .iter() .map(|card| FlipCardState { show_answer: false, - question: card["q"].as_str().unwrap().to_string(), - answer: card["a"].as_str().unwrap().to_string(), + question: card["q"].to_string().into(), + answer: card["a"].to_string().into(), }) .collect(), current_index: 0, // Start at the first card @@ -128,9 +129,9 @@ fn main() { // Render the current flip card vstack(( text(if current_card.show_answer { - current_card.answer.as_str() + ¤t_card.answer } else { - current_card.question.as_str() + ¤t_card.question }) .font_size(12) .padding(Auto), diff --git a/src/views/text.rs b/src/views/text.rs index 04633ed..3878579 100644 --- a/src/views/text.rs +++ b/src/views/text.rs @@ -106,7 +106,7 @@ macro_rules! impl_text { let txt = &format!("{}", self); let vger = &mut args.vger; let origin = vger.text_bounds(txt, Text::DEFAULT_SIZE, None).origin; - + vger.save(); vger.translate([-origin.x, -origin.y]); vger.text(txt, Text::DEFAULT_SIZE, TEXT_COLOR, None); @@ -116,7 +116,7 @@ macro_rules! impl_text { let txt = &format!("{}", self); (args.text_bounds)(txt, Text::DEFAULT_SIZE, None).size } - + fn access( &self, path: &mut IdPath, @@ -131,8 +131,7 @@ macro_rules! impl_text { } } - impl TextModifiers for $ty - { + impl TextModifiers for $ty { fn font_size(self, size: u32) -> Text { Text { text: format!("{}", self), @@ -159,8 +158,7 @@ macro_rules! impl_text { } } } - - } + }; } // XXX: this used to be generic for any Display but @@ -205,8 +203,7 @@ impl DynView for &'static str { } } -impl TextModifiers for &'static str -{ +impl TextModifiers for &'static str { fn font_size(self, size: u32) -> Text { Text { text: format!("{}", self),