1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
#![cfg_attr(not(test), no_std)]
#![cfg_attr(
not(test),
deny(
clippy::indexing_slicing,
clippy::unwrap_used,
clippy::expect_used,
clippy::panic
)
)]
//! `writeable` is a utility crate of the [`ICU4X`] project.
//!
//! It includes [`Writeable`], a core trait representing an object that can be written to a
//! sink implementing `std::fmt::Write`. It is an alternative to `std::fmt::Display` with the
//! addition of a function indicating the number of bytes to be written.
//!
//! `Writeable` improves upon `std::fmt::Display` in two ways:
//!
//! 1. More efficient, since the sink can pre-allocate bytes.
//! 2. Smaller code, since the format machinery can be short-circuited.
//!
//! Types implementing Writeable have a defaulted [`write_to_string`](Writeable::write_to_string)
//! function. If desired, types implementing `Writeable` can manually implement `ToString`
//! to wrap `write_to_string`.
//!
//! # Examples
//!
//! ```
//! use writeable::Writeable;
//! use writeable::LengthHint;
//! use writeable::assert_writeable_eq;
//! use std::fmt;
//!
//! struct WelcomeMessage<'s>{
//! pub name: &'s str,
//! }
//!
//! impl<'s> Writeable for WelcomeMessage<'s> {
//! fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
//! sink.write_str("Hello, ")?;
//! sink.write_str(self.name)?;
//! sink.write_char('!')?;
//! Ok(())
//! }
//!
//! fn write_len(&self) -> LengthHint {
//! // "Hello, " + '!' + length of name
//! LengthHint::exact(8 + self.name.len())
//! }
//! }
//!
//! let message = WelcomeMessage { name: "Alice" };
//! assert_writeable_eq!(&message, "Hello, Alice!");
//! ```
//!
//! [`ICU4X`]: ../icu/index.html
extern crate alloc;
mod impls;
mod ops;
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
/// A hint to help consumers of `Writeable` pre-allocate bytes before they call
/// [`write_to`](Writeable::write_to).
///
/// This behaves like `Iterator::size_hint`: it is a tuple where the first element is the
/// lower bound, and the second element is the upper bound. If the upper bound is `None`
/// either there is no known upper bound, or the upper bound is larger than `usize`.
///
/// `LengthHint` implements std`::ops::{Add, Mul}` and similar traits for easy composition.
/// During computation, the lower bound will saturate at `usize::MAX`, while the upper
/// bound will become `None` if `usize::MAX` is exceeded.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct LengthHint(pub usize, pub Option<usize>);
impl LengthHint {
pub fn undefined() -> Self {
Self(0, None)
}
/// This is the exact length from `write_to`.
pub fn exact(n: usize) -> Self {
Self(n, Some(n))
}
/// This is at least the length from `write_to`.
pub fn at_least(n: usize) -> Self {
Self(n, None)
}
/// This is at most the length from `write_to`.
pub fn at_most(n: usize) -> Self {
Self(0, Some(n))
}
/// The length from `write_to` is in between `n` and `m`.
pub fn between(n: usize, m: usize) -> Self {
Self(Ord::min(n, m), Some(Ord::max(n, m)))
}
/// Returns a recommendation for the number of bytes to pre-allocate.
/// If an upper bound exists, this is used, otherwise the lower bound
/// (which might be 0).
///
/// # Examples
///
/// ```
/// use writeable::Writeable;
///
/// fn pre_allocate_string(w: &impl Writeable) -> String {
/// String::with_capacity(w.write_len().capacity())
/// }
/// ```
pub fn capacity(&self) -> usize {
match self {
Self(lower_bound, None) => *lower_bound,
Self(_lower_bound, Some(upper_bound)) => *upper_bound,
}
}
/// Returns whether the `LengthHint` indicates that the string is exactly 0 bytes long.
pub fn is_zero(&self) -> bool {
self.1 == Some(0)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Part {
pub category: &'static str,
pub value: &'static str,
}
/// A sink that supports annotating parts of the string with `Part`s.
pub trait PartsWrite: fmt::Write {
type SubPartsWrite: PartsWrite + ?Sized;
fn with_part(
&mut self,
part: Part,
f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result,
) -> fmt::Result;
}
/// `Writeable` is an alternative to `std::fmt::Display` with the addition of a length function.
pub trait Writeable {
/// Writes bytes to the given sink. Errors from the sink are bubbled up.
/// The default implementation delegates to `write_to_parts`, and discards any
/// `Part` annotations.
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
struct CoreWriteAsPartsWrite<W: fmt::Write + ?Sized>(W);
impl<W: fmt::Write + ?Sized> fmt::Write for CoreWriteAsPartsWrite<W> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.0.write_str(s)
}
fn write_char(&mut self, c: char) -> fmt::Result {
self.0.write_char(c)
}
}
impl<W: fmt::Write + ?Sized> PartsWrite for CoreWriteAsPartsWrite<W> {
type SubPartsWrite = CoreWriteAsPartsWrite<W>;
fn with_part(
&mut self,
_part: Part,
mut f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result,
) -> fmt::Result {
f(self)
}
}
self.write_to_parts(&mut CoreWriteAsPartsWrite(sink))
}
/// Write bytes and `Part` annotations to the given sink. Errors from the
/// sink are bubbled up. The default implementation delegates to `write_to`,
/// and doesn't produce any `Part` annotations.
fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result {
self.write_to(sink)
}
/// Returns a hint for the number of bytes that will be written to the sink.
///
/// Override this method if it can be computed quickly.
fn write_len(&self) -> LengthHint {
LengthHint::undefined()
}
/// Creates a new `String` with the data from this `Writeable`. Like `ToString`,
/// but smaller and faster.
///
/// The default impl allocates an owned `String`. However, if it is possible to return a
/// borrowed string, overwrite this method to return a `Cow::Borrowed`.
///
/// To remove the `Cow` wrapper, call `.into_owned()`.
///
/// # Examples
///
/// Inspect a `Writeable` before writing it to the sink:
///
/// ```
/// use writeable::Writeable;
/// use core::fmt::{Write, Result};
///
/// fn write_if_ascii<W, S>(w: &W, sink: &mut S) -> Result
/// where
/// W: Writeable + ?Sized,
/// S: Write + ?Sized,
/// {
/// let s = w.write_to_string();
/// if s.is_ascii() {
/// sink.write_str(&s)
/// } else {
/// Ok(())
/// }
/// }
/// ```
///
/// Convert the `Writeable` into a fully owned `String`:
///
/// ```
/// use writeable::Writeable;
///
/// fn make_string(w: &impl Writeable) -> String {
/// w.write_to_string().into_owned()
/// }
/// ```
fn write_to_string(&self) -> Cow<str> {
let mut output = String::with_capacity(self.write_len().capacity());
#[allow(clippy::expect_used)] // TODO(#1668) Clippy exceptions need docs or fixing.
self.write_to(&mut output)
.expect("impl Write for String is infallible");
Cow::Owned(output)
}
}
/// Testing macros for types implementing Writeable. The first argument should be a
/// `Writeable`, the second argument a string, and the third argument (*_parts_eq only)
/// a list of parts (`[(usize, usize, Part)]`).
///
/// The macros tests for equality of string content, parts (*_parts_eq only), and
/// verify the size hint.
///
/// # Examples
///
/// ```
/// # use writeable::Writeable;
/// # use writeable::LengthHint;
/// # use writeable::Part;
/// # use writeable::assert_writeable_eq;
/// # use writeable::assert_writeable_parts_eq;
/// # use std::fmt::{self, Write};
///
/// const WORD: Part = Part { category: "foo", value: "word" };
///
/// struct Demo;
/// impl Writeable for Demo {
/// fn write_to_parts<S: writeable::PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result {
/// sink.with_part(WORD, |w| w.write_str("foo"))
/// }
/// fn write_len(&self) -> LengthHint {
/// LengthHint::exact(3)
/// }
/// }
///
/// assert_writeable_eq!(&Demo, "foo");
/// assert_writeable_eq!(&Demo, "foo", "Message: {}", "Hello World");
///
/// assert_writeable_parts_eq!(&Demo, "foo", [(0, 3, WORD)]);
/// assert_writeable_parts_eq!(&Demo, "foo", [(0, 3, WORD)], "Message: {}", "Hello World");
/// ```
#[macro_export]
macro_rules! assert_writeable_eq {
($actual_writeable:expr, $expected_str:expr $(,)?) => {
$crate::assert_writeable_eq!($actual_writeable, $expected_str, "");
};
($actual_writeable:expr, $expected_str:expr, $($arg:tt)+) => {{
let actual_writeable = &$actual_writeable;
let (actual_str, _) = $crate::writeable_to_parts_for_test(actual_writeable).unwrap();
assert_eq!(actual_str, $expected_str, $($arg)*);
assert_eq!(actual_str, $crate::Writeable::write_to_string(actual_writeable), $($arg)+);
let length_hint = $crate::Writeable::write_len(actual_writeable);
assert!(length_hint.0 <= actual_str.len(), $($arg)*);
if let Some(upper) = length_hint.1 {
assert!(actual_str.len() <= upper, $($arg)*);
}
}};
}
#[macro_export]
macro_rules! assert_writeable_parts_eq {
($actual_writeable:expr, $expected_str:expr, $expected_parts:expr $(,)?) => {
$crate::assert_writeable_parts_eq!($actual_writeable, $expected_str, $expected_parts, "");
};
($actual_writeable:expr, $expected_str:expr, $expected_parts:expr, $($arg:tt)+) => {{
let actual_writeable = &$actual_writeable;
let (actual_str, actual_parts) = $crate::writeable_to_parts_for_test(actual_writeable).unwrap();
assert_eq!(actual_str, $expected_str, $($arg)+);
assert_eq!(actual_str, $crate::Writeable::write_to_string(actual_writeable), $($arg)+);
assert_eq!(actual_parts, $expected_parts, $($arg)+);
let length_hint = $crate::Writeable::write_len(actual_writeable);
assert!(length_hint.0 <= actual_str.len(), $($arg)+);
if let Some(upper) = length_hint.1 {
assert!(actual_str.len() <= upper, $($arg)+);
}
}};
}
#[doc(hidden)]
#[allow(clippy::type_complexity)]
pub fn writeable_to_parts_for_test<W: Writeable>(
writeable: &W,
) -> Result<(String, Vec<(usize, usize, Part)>), fmt::Error> {
struct State {
string: alloc::string::String,
parts: Vec<(usize, usize, Part)>,
}
impl fmt::Write for State {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.string.write_str(s)
}
fn write_char(&mut self, c: char) -> fmt::Result {
self.string.write_char(c)
}
}
impl PartsWrite for State {
type SubPartsWrite = Self;
fn with_part(
&mut self,
part: Part,
mut f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result,
) -> fmt::Result {
let start = self.string.len();
f(self)?;
self.parts.push((start, self.string.len(), part));
Ok(())
}
}
let mut state = State {
string: alloc::string::String::new(),
parts: Vec::new(),
};
writeable.write_to_parts(&mut state)?;
// Sort by first open and last closed
state
.parts
.sort_unstable_by_key(|(begin, end, _)| (*begin, end.wrapping_neg()));
Ok((state.string, state.parts))
}