Skip to content

Commit 898708b

Browse files
committed
use IntoIterator for text builders
1 parent 7431c56 commit 898708b

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

crates/bevy_text/src/text.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use cosmic_text::{Buffer, Metrics};
99
use serde::{Deserialize, Serialize};
1010
use smallvec::SmallVec;
1111

12-
use crate::{Font, TextLayoutInfo, TextRoot};
12+
use crate::{Font, TextLayoutInfo, TextRoot, TextSpanComponent};
1313
pub use cosmic_text::{
1414
self, FamilyOwned as FontFamily, Stretch as FontStretch, Style as FontStyle,
1515
Weight as FontWeight,
@@ -286,15 +286,18 @@ pub trait TextBuilderExt {
286286
/// If no spans are provided then a default text entity will be spawned.
287287
///
288288
/// Returns an [`EntityCommands`] for the root entity.
289-
fn spawn_text_block<R: TextRoot>(&mut self, spans: Vec<(String, TextStyle)>) -> EntityCommands;
289+
fn spawn_text_block<R: TextRoot>(
290+
&mut self,
291+
spans: impl IntoIterator<Item = (String, TextStyle)>,
292+
) -> EntityCommands;
290293
}
291294

292295
impl TextBuilderExt for Commands<'_, '_> {
293296
fn spawn_text_block<R: TextRoot>(
294297
&mut self,
295-
mut spans: Vec<(String, TextStyle)>,
298+
spans: impl IntoIterator<Item = (String, TextStyle)>,
296299
) -> EntityCommands {
297-
let mut spans = spans.drain(..);
300+
let mut spans = spans.into_iter();
298301

299302
// Root of the block.
300303
let first = spans.next().unwrap_or_default();
@@ -312,9 +315,9 @@ impl TextBuilderExt for Commands<'_, '_> {
312315
impl TextBuilderExt for EntityCommands<'_> {
313316
fn spawn_text_block<R: TextRoot>(
314317
&mut self,
315-
mut spans: Vec<(String, TextStyle)>,
318+
spans: impl IntoIterator<Item = (String, TextStyle)>,
316319
) -> EntityCommands {
317-
let mut spans = spans.drain(..);
320+
let mut spans = spans.into_iter();
318321

319322
// Root of the block.
320323
let first = spans.next().unwrap_or_default();
@@ -332,13 +335,19 @@ impl TextBuilderExt for EntityCommands<'_> {
332335
/// Provides convenience methods for adding text spans to a text block.
333336
pub trait TextSpanBuilderExt {
334337
/// Adds a flat list of spans as children of the current entity.
335-
fn with_spans<S: Component>(&mut self, spans: Vec<(S, TextStyle)>) -> &mut Self;
338+
fn with_spans<S: TextSpanComponent>(
339+
&mut self,
340+
spans: impl IntoIterator<Item = (String, TextStyle)>,
341+
) -> &mut Self;
336342
}
337343

338344
impl TextSpanBuilderExt for EntityCommands<'_> {
339-
fn with_spans<S: Component>(&mut self, mut spans: Vec<(S, TextStyle)>) -> &mut Self {
340-
for (span, style) in spans.drain(..) {
341-
self.with_child((span, style));
345+
fn with_spans<S: TextSpanComponent>(
346+
&mut self,
347+
spans: impl IntoIterator<Item = (String, TextStyle)>,
348+
) -> &mut Self {
349+
for (span, style) in spans.into_iter() {
350+
self.with_child((S::from(span), style));
342351
}
343352
self
344353
}

crates/bevy_text/src/text2d.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::pipeline::CosmicFontSystem;
22
use crate::{
33
ComputedTextBlock, Font, FontAtlasSets, LineBreak, PositionedGlyph, SwashCache, TextBlock,
44
TextBounds, TextError, TextLayoutInfo, TextPipeline, TextReader, TextRoot, TextSpanAccess,
5-
TextStyle, TextWriter, YAxisOrientation,
5+
TextSpanComponent, TextStyle, TextWriter, YAxisOrientation,
66
};
77
use bevy_asset::Assets;
88
use bevy_color::LinearRgba;
@@ -175,6 +175,8 @@ impl TextSpan2d {
175175
}
176176
}
177177

178+
impl TextSpanComponent for TextSpan2d {}
179+
178180
impl TextSpanAccess for TextSpan2d {
179181
fn read_span(&self) -> &str {
180182
self.as_str()

crates/bevy_text/src/text_access.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ pub trait TextSpanAccess: Component {
1717
/// Helper trait for the root text component in a text block.
1818
pub trait TextRoot: TextSpanAccess + From<String> {
1919
/// Component type for spans of text blocks starting with the root component.
20-
type Span: TextSpanAccess + From<String>;
20+
type Span: TextSpanComponent;
2121
}
2222

23+
/// Helper trait for the text span components in a text block.
24+
pub trait TextSpanComponent: TextSpanAccess + From<String> {}
25+
2326
#[derive(Resource, Default)]
2427
pub(crate) struct TextIterScratch {
2528
stack: Vec<(&'static Children, usize)>,

crates/bevy_ui/src/widget/text.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use bevy_sprite::TextureAtlasLayout;
2020
use bevy_text::{
2121
scale_value, ComputedTextBlock, CosmicFontSystem, Font, FontAtlasSets, LineBreak, SwashCache,
2222
TextBlock, TextBounds, TextError, TextLayoutInfo, TextMeasureInfo, TextPipeline, TextReader,
23-
TextRoot, TextSpanAccess, TextStyle, TextWriter, YAxisOrientation,
23+
TextRoot, TextSpanAccess, TextSpanComponent, TextStyle, TextWriter, YAxisOrientation,
2424
};
2525
use bevy_transform::components::Transform;
2626
use bevy_utils::{tracing::error, Entry};
@@ -192,6 +192,8 @@ impl TextSpan {
192192
}
193193
}
194194

195+
impl TextSpanComponent for TextSpan {}
196+
195197
impl TextSpanAccess for TextSpan {
196198
fn read_span(&self) -> &str {
197199
self.as_str()

examples/ui/text_debug.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use bevy::{
66
color::palettes::css::*,
77
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
88
prelude::*,
9-
text::TextBuilderExt,
9+
text::TextSpanBuilderExt,
1010
ui::widget::UiTextWriter,
1111
window::PresentMode,
1212
};
@@ -162,7 +162,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
162162
},
163163
TextChanges
164164
))
165-
.with_spans::<TextSpan>(vec![
165+
.with_spans::<TextSpan>([
166166
(
167167
"\nThis text changes in the bottom right".into(),
168168
TextStyle {
@@ -198,7 +198,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
198198
},
199199
),
200200
(
201-
TextSpan::empty(),
201+
String::default(),
202202
TextStyle {
203203
font: font.clone(),
204204
font_size: 21.0,
@@ -216,7 +216,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
216216
},
217217
),
218218
(
219-
TextSpan::empty(),
219+
String::default(),
220220
TextStyle {
221221
font: font.clone(),
222222
font_size: 21.0,

0 commit comments

Comments
 (0)