Skip to content

Commit dd0e253

Browse files
authored
Generate to_string concrete fn in impl_display_with_writeable (#5827)
Fixes #4590 I made it the default behavior.
1 parent e7439f7 commit dd0e253

File tree

8 files changed

+25
-5
lines changed

8 files changed

+25
-5
lines changed

components/locale_core/src/databake.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

55
use crate::LanguageIdentifier;
6-
use alloc::string::ToString;
76
use databake::*;
87

98
impl Bake for LanguageIdentifier {

components/locale_core/src/preferences/extensions/unicode/keywords/regional_subdivision.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{
88
extensions::unicode::{SubdivisionId, Value},
99
subtags::Subtag,
1010
};
11-
use alloc::string::ToString;
1211

1312
struct_keyword!(
1413
/// A Unicode Subdivision Identifier defines a regional subdivision used for locales.

components/locale_core/src/serde.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

55
use crate::LanguageIdentifier;
6-
use alloc::string::ToString;
76
use serde::{Deserialize, Deserializer, Serialize, Serializer};
87

98
impl Serialize for LanguageIdentifier {

ffi/capi/src/decimal.rs

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub mod ffi {
148148
)]
149149
#[diplomat::rust_link(icu::decimal::FormattedFixedDecimal, Struct, hidden)]
150150
#[diplomat::rust_link(icu::decimal::FormattedFixedDecimal::write_to, FnInStruct, hidden)]
151+
#[diplomat::rust_link(icu::decimal::FormattedFixedDecimal::to_string, FnInStruct, hidden)]
151152
pub fn format(&self, value: &FixedDecimal, write: &mut diplomat_runtime::DiplomatWrite) {
152153
let _infallible = self.0.format(&value.0).write_to(write);
153154
}

ffi/capi/src/fixed_decimal.rs

+1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ pub mod ffi {
350350

351351
/// Format the [`FixedDecimal`] as a string.
352352
#[diplomat::rust_link(fixed_decimal::FixedDecimal::write_to, FnInStruct)]
353+
#[diplomat::rust_link(fixed_decimal::FixedDecimal::to_string, FnInStruct, hidden)]
353354
#[diplomat::attr(auto, stringifier)]
354355
pub fn to_string(&self, to: &mut diplomat_runtime::DiplomatWrite) {
355356
let _ = self.0.write_to(to);

ffi/capi/src/locale_core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub mod ffi {
146146
}
147147
/// Returns a string representation of [`Locale`].
148148
#[diplomat::rust_link(icu::locale::Locale::write_to, FnInStruct)]
149+
#[diplomat::rust_link(icu::locale::Locale::to_string, FnInStruct, hidden)]
149150
#[diplomat::attr(auto, stringifier)]
150151
pub fn to_string(&self, write: &mut diplomat_runtime::DiplomatWrite) {
151152
let _infallible = self.0.write_to(write);

utils/writeable/README.md

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/writeable/src/lib.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
//! // Types implementing `Writeable` are recommended to also implement `fmt::Display`.
6161
//! // This can be simply done by redirecting to the `Writeable` implementation:
6262
//! writeable::impl_display_with_writeable!(WelcomeMessage<'_>);
63+
//! assert_eq!(message.to_string(), "Hello, Alice!");
6364
//! ```
6465
//!
6566
//! [`ICU4X`]: ../icu/index.html
@@ -116,10 +117,11 @@ pub mod adapters {
116117
}
117118
}
118119

119-
#[doc(hidden)] // for testing
120+
#[doc(hidden)] // for testing and macros
120121
pub mod _internal {
121122
pub use super::testing::try_writeable_to_parts_for_test;
122123
pub use super::testing::writeable_to_parts_for_test;
124+
pub use alloc::string::String;
123125
}
124126

125127
/// A hint to help consumers of `Writeable` pre-allocate bytes before they call
@@ -314,9 +316,13 @@ pub trait Writeable {
314316
/// It's recommended to do this for every [`Writeable`] type, as it will add
315317
/// support for `core::fmt` features like [`fmt!`](std::fmt),
316318
/// [`print!`](std::print), [`write!`](std::write), etc.
319+
///
320+
/// This macro also adds a concrete `to_string` function. This function will shadow the
321+
/// standard library `ToString`, using the more efficient writeable-based code path.
322+
/// To add only `Display`, use the `@display` macro variant.
317323
#[macro_export]
318324
macro_rules! impl_display_with_writeable {
319-
($type:ty) => {
325+
(@display, $type:ty) => {
320326
/// This trait is implemented for compatibility with [`fmt!`](alloc::fmt).
321327
/// To create a string, [`Writeable::write_to_string`] is usually more efficient.
322328
impl core::fmt::Display for $type {
@@ -326,6 +332,19 @@ macro_rules! impl_display_with_writeable {
326332
}
327333
}
328334
};
335+
($type:ty) => {
336+
$crate::impl_display_with_writeable!(@display, $type);
337+
impl $type {
338+
/// Converts the given value to a `String`.
339+
///
340+
/// Under the hood, this uses an efficient [`Writeable`] implementation.
341+
/// However, in order to avoid allocating a string, it is more efficient
342+
/// to use [`Writeable`] directly.
343+
pub fn to_string(&self) -> $crate::_internal::String {
344+
$crate::Writeable::write_to_string(self).into_owned()
345+
}
346+
}
347+
};
329348
}
330349

331350
/// Testing macros for types implementing [`Writeable`].

0 commit comments

Comments
 (0)