+99
| // 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 ). | ||
| use core::cmp::Ordering; | ||
| use core::fmt; | ||
| pub(crate) struct WriteComparator<'a> { | ||
| string: &'a [u8], | ||
| result: Ordering, | ||
| } | ||
| /// This is an infallible impl. Functions always return Ok, not Err. | ||
| impl<'a> fmt::Write for WriteComparator<'a> { | ||
| #[inline] | ||
| fn write_str(&mut self, other: &str) -> fmt::Result { | ||
| if self.result != Ordering::Equal { | ||
| return Ok(()); | ||
| } | ||
| let cmp_len = core::cmp::min(other.len(), self.string.len()); | ||
| let (this, remainder) = self.string.split_at(cmp_len); | ||
| self.string = remainder; | ||
| self.result = this.cmp(other.as_bytes()); | ||
| Ok(()) | ||
| } | ||
| } | ||
| impl<'a> WriteComparator<'a> { | ||
| #[inline] | ||
| pub fn new(string: &'a (impl AsRef<[u8]> + ?Sized)) -> Self { | ||
| Self { | ||
| string: string.as_ref(), | ||
| result: Ordering::Equal, | ||
| } | ||
| } | ||
| #[inline] | ||
| pub fn finish(self) -> Ordering { | ||
| if matches!(self.result, Ordering::Equal) && !self.string.is_empty() { | ||
| // Self is longer than Other | ||
| Ordering::Greater | ||
| } else { | ||
| self.result | ||
| } | ||
| } | ||
| } | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use core::fmt::Write; | ||
| mod data { | ||
| include!("../tests/data/data.rs"); | ||
| } | ||
| #[test] | ||
| fn test_write_char() { | ||
| for a in data::KEBAB_CASE_STRINGS { | ||
| for b in data::KEBAB_CASE_STRINGS { | ||
| let mut wc = WriteComparator::new(a); | ||
| for ch in b.chars() { | ||
| wc.write_char(ch).unwrap(); | ||
| } | ||
| assert_eq!(a.cmp(b), wc.finish(), "{a} <=> {b}"); | ||
| } | ||
| } | ||
| } | ||
| #[test] | ||
| fn test_write_str() { | ||
| for a in data::KEBAB_CASE_STRINGS { | ||
| for b in data::KEBAB_CASE_STRINGS { | ||
| let mut wc = WriteComparator::new(a); | ||
| wc.write_str(b).unwrap(); | ||
| assert_eq!(a.cmp(b), wc.finish(), "{a} <=> {b}"); | ||
| } | ||
| } | ||
| } | ||
| #[test] | ||
| fn test_mixed() { | ||
| for a in data::KEBAB_CASE_STRINGS { | ||
| for b in data::KEBAB_CASE_STRINGS { | ||
| let mut wc = WriteComparator::new(a); | ||
| let mut first = true; | ||
| for substr in b.split('-') { | ||
| if first { | ||
| first = false; | ||
| } else { | ||
| wc.write_char('-').unwrap(); | ||
| } | ||
| wc.write_str(substr).unwrap(); | ||
| } | ||
| assert_eq!(a.cmp(b), wc.finish(), "{a} <=> {b}"); | ||
| } | ||
| } | ||
| } | ||
| } |
| // 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 ). | ||
| use crate::*; | ||
| use ::either::Either; | ||
| /// A [`Writeable`] impl that delegates to one type or another type. | ||
| impl<W0, W1> Writeable for Either<W0, W1> | ||
| where | ||
| W0: Writeable, | ||
| W1: Writeable, | ||
| { | ||
| fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { | ||
| match self { | ||
| Either::Left(w) => w.write_to(sink), | ||
| Either::Right(w) => w.write_to(sink), | ||
| } | ||
| } | ||
| fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result { | ||
| match self { | ||
| Either::Left(w) => w.write_to_parts(sink), | ||
| Either::Right(w) => w.write_to_parts(sink), | ||
| } | ||
| } | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| match self { | ||
| Either::Left(w) => w.writeable_length_hint(), | ||
| Either::Right(w) => w.writeable_length_hint(), | ||
| } | ||
| } | ||
| fn write_to_string(&self) -> Cow<str> { | ||
| match self { | ||
| Either::Left(w) => w.write_to_string(), | ||
| Either::Right(w) => w.write_to_string(), | ||
| } | ||
| } | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| match self { | ||
| Either::Left(w) => w.writeable_cmp_bytes(other), | ||
| Either::Right(w) => w.writeable_cmp_bytes(other), | ||
| } | ||
| } | ||
| } |
| // 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 ). | ||
| use crate::*; | ||
| #[derive(Debug)] | ||
| #[allow(clippy::exhaustive_structs)] // newtype | ||
| pub struct CoreWriteAsPartsWrite<W: fmt::Write + ?Sized>(pub W); | ||
| impl<W: fmt::Write + ?Sized> fmt::Write for CoreWriteAsPartsWrite<W> { | ||
| #[inline] | ||
| fn write_str(&mut self, s: &str) -> fmt::Result { | ||
| self.0.write_str(s) | ||
| } | ||
| #[inline] | ||
| 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>; | ||
| #[inline] | ||
| fn with_part( | ||
| &mut self, | ||
| _part: Part, | ||
| mut f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result, | ||
| ) -> fmt::Result { | ||
| f(self) | ||
| } | ||
| } |
| // 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 ). | ||
| use crate::*; | ||
| use alloc::string::String; | ||
| use alloc::vec::Vec; | ||
| pub(crate) struct TestWriter { | ||
| pub(crate) string: String, | ||
| pub(crate) parts: Vec<(usize, usize, Part)>, | ||
| } | ||
| impl TestWriter { | ||
| pub(crate) fn finish(mut self) -> (String, Vec<(usize, usize, Part)>) { | ||
| // Sort by first open and last closed | ||
| self.parts | ||
| .sort_unstable_by_key(|(begin, end, _)| (*begin, end.wrapping_neg())); | ||
| (self.string, self.parts) | ||
| } | ||
| } | ||
| impl fmt::Write for TestWriter { | ||
| 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 TestWriter { | ||
| 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)?; | ||
| let end = self.string.len(); | ||
| if start < end { | ||
| self.parts.push((start, end, part)); | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
| #[allow(clippy::type_complexity)] | ||
| pub fn writeable_to_parts_for_test<W: Writeable>( | ||
| writeable: &W, | ||
| ) -> (String, Vec<(usize, usize, Part)>) { | ||
| let mut writer = TestWriter { | ||
| string: alloc::string::String::new(), | ||
| parts: Vec::new(), | ||
| }; | ||
| #[allow(clippy::expect_used)] // for test code | ||
| writeable | ||
| .write_to_parts(&mut writer) | ||
| .expect("String writer infallible"); | ||
| writer.finish() | ||
| } | ||
| #[allow(clippy::type_complexity)] | ||
| pub fn try_writeable_to_parts_for_test<W: TryWriteable>( | ||
| writeable: &W, | ||
| ) -> (String, Vec<(usize, usize, Part)>, Option<W::Error>) { | ||
| let mut writer = TestWriter { | ||
| string: alloc::string::String::new(), | ||
| parts: Vec::new(), | ||
| }; | ||
| #[allow(clippy::expect_used)] // for test code | ||
| let result = writeable | ||
| .try_write_to_parts(&mut writer) | ||
| .expect("String writer infallible"); | ||
| let (actual_str, actual_parts) = writer.finish(); | ||
| (actual_str, actual_parts, result.err()) | ||
| } |
| // 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 ). | ||
| use super::*; | ||
| use crate::parts_write_adapter::CoreWriteAsPartsWrite; | ||
| use core::{cmp::Ordering, convert::Infallible}; | ||
| /// A writeable object that can fail while writing. | ||
| /// | ||
| /// The default [`Writeable`] trait returns a [`fmt::Error`], which originates from the sink. | ||
| /// In contrast, this trait allows the _writeable itself_ to trigger an error as well. | ||
| /// | ||
| /// Implementations are expected to always make a _best attempt_ at writing to the sink | ||
| /// and should write replacement values in the error state. Therefore, the returned `Result` | ||
| /// can be safely ignored to emulate a "lossy" mode. | ||
| /// | ||
| /// Any error substrings should be annotated with [`Part::ERROR`]. | ||
| /// | ||
| /// # Implementer Notes | ||
| /// | ||
| /// This trait requires that implementers make a _best attempt_ at writing to the sink, | ||
| /// _even in the error state_, such as with a placeholder or fallback string. | ||
| /// | ||
| /// In [`TryWriteable::try_write_to_parts()`], error substrings should be annotated with | ||
| /// [`Part::ERROR`]. Because of this, writing to parts is not default-implemented like | ||
| /// it is on [`Writeable`]. | ||
| /// | ||
| /// The trait is implemented on [`Result<T, E>`] where `T` and `E` both implement [`Writeable`]; | ||
| /// In the `Ok` case, `T` is written, and in the `Err` case, `E` is written as a fallback value. | ||
| /// This impl, which writes [`Part::ERROR`], can be used as a basis for more advanced impls. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Implementing on a custom type: | ||
| /// | ||
| /// ``` | ||
| /// use core::fmt; | ||
| /// use writeable::LengthHint; | ||
| /// use writeable::PartsWrite; | ||
| /// use writeable::TryWriteable; | ||
| /// | ||
| /// #[derive(Debug, PartialEq, Eq)] | ||
| /// enum HelloWorldWriteableError { | ||
| /// MissingName, | ||
| /// } | ||
| /// | ||
| /// #[derive(Debug, PartialEq, Eq)] | ||
| /// struct HelloWorldWriteable { | ||
| /// pub name: Option<&'static str>, | ||
| /// } | ||
| /// | ||
| /// impl TryWriteable for HelloWorldWriteable { | ||
| /// type Error = HelloWorldWriteableError; | ||
| /// | ||
| /// fn try_write_to_parts<S: PartsWrite + ?Sized>( | ||
| /// &self, | ||
| /// sink: &mut S, | ||
| /// ) -> Result<Result<(), Self::Error>, fmt::Error> { | ||
| /// sink.write_str("Hello, ")?; | ||
| /// // Use `impl TryWriteable for Result` to generate the error part: | ||
| /// let err = self.name.ok_or("nobody").try_write_to_parts(sink)?.err(); | ||
| /// sink.write_char('!')?; | ||
| /// // Return a doubly-wrapped Result. | ||
| /// // The outer Result is for fmt::Error, handled by the `?`s above. | ||
| /// // The inner Result is for our own Self::Error. | ||
| /// if err.is_none() { | ||
| /// Ok(Ok(())) | ||
| /// } else { | ||
| /// Ok(Err(HelloWorldWriteableError::MissingName)) | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// fn writeable_length_hint(&self) -> LengthHint { | ||
| /// self.name.ok_or("nobody").writeable_length_hint() + 8 | ||
| /// } | ||
| /// } | ||
| /// | ||
| /// // Success case: | ||
| /// writeable::assert_try_writeable_eq!( | ||
| /// HelloWorldWriteable { | ||
| /// name: Some("Alice") | ||
| /// }, | ||
| /// "Hello, Alice!" | ||
| /// ); | ||
| /// | ||
| /// // Failure case, including the ERROR part: | ||
| /// writeable::assert_try_writeable_parts_eq!( | ||
| /// HelloWorldWriteable { name: None }, | ||
| /// "Hello, nobody!", | ||
| /// Err(HelloWorldWriteableError::MissingName), | ||
| /// [(7, 13, writeable::Part::ERROR)] | ||
| /// ); | ||
| /// ``` | ||
| pub trait TryWriteable { | ||
| type Error; | ||
| /// Writes the content of this writeable to a sink. | ||
| /// | ||
| /// If the sink hits an error, writing immediately ends, | ||
| /// `Err(`[`fmt::Error`]`)` is returned, and the sink does not contain valid output. | ||
| /// | ||
| /// If the writeable hits an error, writing is continued with a replacement value, | ||
| /// `Ok(Err(`[`TryWriteable::Error`]`))` is returned, and the caller may continue using the sink. | ||
| /// | ||
| /// # Lossy Mode | ||
| /// | ||
| /// The [`fmt::Error`] should always be handled, but the [`TryWriteable::Error`] can be | ||
| /// ignored if a fallback string is desired instead of an error. | ||
| /// | ||
| /// To handle the sink error, but not the writeable error, write: | ||
| /// | ||
| /// ``` | ||
| /// # use writeable::TryWriteable; | ||
| /// # let my_writeable: Result<&str, &str> = Ok(""); | ||
| /// # let mut sink = String::new(); | ||
| /// let _ = my_writeable.try_write_to(&mut sink)?; | ||
| /// # Ok::<(), core::fmt::Error>(()) | ||
| /// ``` | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// The following examples use `Result<&str, usize>`, which implements [`TryWriteable`] because both `&str` and `usize` do. | ||
| /// | ||
| /// Success case: | ||
| /// | ||
| /// ``` | ||
| /// use writeable::TryWriteable; | ||
| /// | ||
| /// let w: Result<&str, usize> = Ok("success"); | ||
| /// let mut sink = String::new(); | ||
| /// let result = w.try_write_to(&mut sink); | ||
| /// | ||
| /// assert_eq!(result, Ok(Ok(()))); | ||
| /// assert_eq!(sink, "success"); | ||
| /// ``` | ||
| /// | ||
| /// Failure case: | ||
| /// | ||
| /// ``` | ||
| /// use writeable::TryWriteable; | ||
| /// | ||
| /// let w: Result<&str, usize> = Err(44); | ||
| /// let mut sink = String::new(); | ||
| /// let result = w.try_write_to(&mut sink); | ||
| /// | ||
| /// assert_eq!(result, Ok(Err(44))); | ||
| /// assert_eq!(sink, "44"); | ||
| /// ``` | ||
| fn try_write_to<W: fmt::Write + ?Sized>( | ||
| &self, | ||
| sink: &mut W, | ||
| ) -> Result<Result<(), Self::Error>, fmt::Error> { | ||
| self.try_write_to_parts(&mut CoreWriteAsPartsWrite(sink)) | ||
| } | ||
| /// Writes the content of this writeable to a sink with parts (annotations). | ||
| /// | ||
| /// For more information, see: | ||
| /// | ||
| /// - [`TryWriteable::try_write_to()`] for the general behavior. | ||
| /// - [`TryWriteable`] for an example with parts. | ||
| /// - [`Part`] for more about parts. | ||
| fn try_write_to_parts<S: PartsWrite + ?Sized>( | ||
| &self, | ||
| sink: &mut S, | ||
| ) -> Result<Result<(), Self::Error>, fmt::Error>; | ||
| /// Returns a hint for the number of UTF-8 bytes that will be written to the sink. | ||
| /// | ||
| /// This function returns the length of the "lossy mode" string; for more information, | ||
| /// see [`TryWriteable::try_write_to()`]. | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| LengthHint::undefined() | ||
| } | ||
| /// Writes the content of this writeable to a string. | ||
| /// | ||
| /// In the failure case, this function returns the error and the best-effort string ("lossy mode"). | ||
| /// | ||
| /// Examples | ||
| /// | ||
| /// ``` | ||
| /// # use std::borrow::Cow; | ||
| /// # use writeable::TryWriteable; | ||
| /// // use the best-effort string | ||
| /// let r1: Cow<str> = Ok::<&str, u8>("ok") | ||
| /// .try_write_to_string() | ||
| /// .unwrap_or_else(|(_, s)| s); | ||
| /// // propagate the error | ||
| /// let r2: Result<Cow<str>, u8> = Ok::<&str, u8>("ok") | ||
| /// .try_write_to_string() | ||
| /// .map_err(|(e, _)| e); | ||
| /// ``` | ||
| fn try_write_to_string(&self) -> Result<Cow<str>, (Self::Error, Cow<str>)> { | ||
| let hint = self.writeable_length_hint(); | ||
| if hint.is_zero() { | ||
| return Ok(Cow::Borrowed("")); | ||
| } | ||
| let mut output = String::with_capacity(hint.capacity()); | ||
| match self | ||
| .try_write_to(&mut output) | ||
| .unwrap_or_else(|fmt::Error| Ok(())) | ||
| { | ||
| Ok(()) => Ok(Cow::Owned(output)), | ||
| Err(e) => Err((e, Cow::Owned(output))), | ||
| } | ||
| } | ||
| /// Compares the content of this writeable to a byte slice. | ||
| /// | ||
| /// This function compares the "lossy mode" string; for more information, | ||
| /// see [`TryWriteable::try_write_to()`]. | ||
| /// | ||
| /// For more information, see [`Writeable::writeable_cmp_bytes()`]. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use core::cmp::Ordering; | ||
| /// use core::fmt; | ||
| /// use writeable::TryWriteable; | ||
| /// # use writeable::PartsWrite; | ||
| /// # use writeable::LengthHint; | ||
| /// | ||
| /// #[derive(Debug, PartialEq, Eq)] | ||
| /// enum HelloWorldWriteableError { | ||
| /// MissingName | ||
| /// } | ||
| /// | ||
| /// #[derive(Debug, PartialEq, Eq)] | ||
| /// struct HelloWorldWriteable { | ||
| /// pub name: Option<&'static str> | ||
| /// } | ||
| /// | ||
| /// impl TryWriteable for HelloWorldWriteable { | ||
| /// type Error = HelloWorldWriteableError; | ||
| /// // see impl in TryWriteable docs | ||
| /// # fn try_write_to_parts<S: PartsWrite + ?Sized>( | ||
| /// # &self, | ||
| /// # sink: &mut S, | ||
| /// # ) -> Result<Result<(), Self::Error>, fmt::Error> { | ||
| /// # sink.write_str("Hello, ")?; | ||
| /// # // Use `impl TryWriteable for Result` to generate the error part: | ||
| /// # let _ = self.name.ok_or("nobody").try_write_to_parts(sink)?; | ||
| /// # sink.write_char('!')?; | ||
| /// # // Return a doubly-wrapped Result. | ||
| /// # // The outer Result is for fmt::Error, handled by the `?`s above. | ||
| /// # // The inner Result is for our own Self::Error. | ||
| /// # if self.name.is_some() { | ||
| /// # Ok(Ok(())) | ||
| /// # } else { | ||
| /// # Ok(Err(HelloWorldWriteableError::MissingName)) | ||
| /// # } | ||
| /// # } | ||
| /// } | ||
| /// | ||
| /// // Success case: | ||
| /// let writeable = HelloWorldWriteable { name: Some("Alice") }; | ||
| /// let writeable_str = writeable.try_write_to_string().expect("name is Some"); | ||
| /// | ||
| /// assert_eq!(Ordering::Equal, writeable.writeable_cmp_bytes(b"Hello, Alice!")); | ||
| /// | ||
| /// assert_eq!(Ordering::Greater, writeable.writeable_cmp_bytes(b"Alice!")); | ||
| /// assert_eq!(Ordering::Greater, (*writeable_str).cmp("Alice!")); | ||
| /// | ||
| /// assert_eq!(Ordering::Less, writeable.writeable_cmp_bytes(b"Hello, Bob!")); | ||
| /// assert_eq!(Ordering::Less, (*writeable_str).cmp("Hello, Bob!")); | ||
| /// | ||
| /// // Failure case: | ||
| /// let writeable = HelloWorldWriteable { name: None }; | ||
| /// let mut writeable_str = String::new(); | ||
| /// let _ = writeable.try_write_to(&mut writeable_str).expect("write to String is infallible"); | ||
| /// | ||
| /// assert_eq!(Ordering::Equal, writeable.writeable_cmp_bytes(b"Hello, nobody!")); | ||
| /// | ||
| /// assert_eq!(Ordering::Greater, writeable.writeable_cmp_bytes(b"Hello, alice!")); | ||
| /// assert_eq!(Ordering::Greater, (*writeable_str).cmp("Hello, alice!")); | ||
| /// | ||
| /// assert_eq!(Ordering::Less, writeable.writeable_cmp_bytes(b"Hello, zero!")); | ||
| /// assert_eq!(Ordering::Less, (*writeable_str).cmp("Hello, zero!")); | ||
| /// ``` | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering { | ||
| let mut wc = cmp::WriteComparator::new(other); | ||
| let _ = self | ||
| .try_write_to(&mut wc) | ||
| .unwrap_or_else(|fmt::Error| Ok(())); | ||
| wc.finish().reverse() | ||
| } | ||
| } | ||
| impl<T, E> TryWriteable for Result<T, E> | ||
| where | ||
| T: Writeable, | ||
| E: Writeable + Clone, | ||
| { | ||
| type Error = E; | ||
| #[inline] | ||
| fn try_write_to<W: fmt::Write + ?Sized>( | ||
| &self, | ||
| sink: &mut W, | ||
| ) -> Result<Result<(), Self::Error>, fmt::Error> { | ||
| match self { | ||
| Ok(t) => t.write_to(sink).map(Ok), | ||
| Err(e) => e.write_to(sink).map(|()| Err(e.clone())), | ||
| } | ||
| } | ||
| #[inline] | ||
| fn try_write_to_parts<S: PartsWrite + ?Sized>( | ||
| &self, | ||
| sink: &mut S, | ||
| ) -> Result<Result<(), Self::Error>, fmt::Error> { | ||
| match self { | ||
| Ok(t) => t.write_to_parts(sink).map(Ok), | ||
| Err(e) => sink | ||
| .with_part(Part::ERROR, |sink| e.write_to_parts(sink)) | ||
| .map(|()| Err(e.clone())), | ||
| } | ||
| } | ||
| #[inline] | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| match self { | ||
| Ok(t) => t.writeable_length_hint(), | ||
| Err(e) => e.writeable_length_hint(), | ||
| } | ||
| } | ||
| #[inline] | ||
| fn try_write_to_string(&self) -> Result<Cow<str>, (Self::Error, Cow<str>)> { | ||
| match self { | ||
| Ok(t) => Ok(t.write_to_string()), | ||
| Err(e) => Err((e.clone(), e.write_to_string())), | ||
| } | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> Ordering { | ||
| match self { | ||
| Ok(t) => t.writeable_cmp_bytes(other), | ||
| Err(e) => e.writeable_cmp_bytes(other), | ||
| } | ||
| } | ||
| } | ||
| /// A wrapper around [`TryWriteable`] that implements [`Writeable`] | ||
| /// if [`TryWriteable::Error`] is [`Infallible`]. | ||
| #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
| #[repr(transparent)] | ||
| #[allow(clippy::exhaustive_structs)] // transparent newtype | ||
| pub struct TryWriteableInfallibleAsWriteable<T>(pub T); | ||
| impl<T> Writeable for TryWriteableInfallibleAsWriteable<T> | ||
| where | ||
| T: TryWriteable<Error = Infallible>, | ||
| { | ||
| #[inline] | ||
| fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { | ||
| match self.0.try_write_to(sink) { | ||
| Ok(Ok(())) => Ok(()), | ||
| Ok(Err(infallible)) => match infallible {}, | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| #[inline] | ||
| fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result { | ||
| match self.0.try_write_to_parts(sink) { | ||
| Ok(Ok(())) => Ok(()), | ||
| Ok(Err(infallible)) => match infallible {}, | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| #[inline] | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| self.0.writeable_length_hint() | ||
| } | ||
| #[inline] | ||
| fn write_to_string(&self) -> Cow<str> { | ||
| match self.0.try_write_to_string() { | ||
| Ok(s) => s, | ||
| Err((infallible, _)) => match infallible {}, | ||
| } | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| self.0.writeable_cmp_bytes(other) | ||
| } | ||
| } | ||
| impl<T> fmt::Display for TryWriteableInfallibleAsWriteable<T> | ||
| where | ||
| T: TryWriteable<Error = Infallible>, | ||
| { | ||
| #[inline] | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| self.write_to(f) | ||
| } | ||
| } | ||
| /// A wrapper around [`Writeable`] that implements [`TryWriteable`] | ||
| /// with [`TryWriteable::Error`] set to [`Infallible`]. | ||
| #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
| #[repr(transparent)] | ||
| #[allow(clippy::exhaustive_structs)] // transparent newtype | ||
| pub struct WriteableAsTryWriteableInfallible<T>(pub T); | ||
| impl<T> TryWriteable for WriteableAsTryWriteableInfallible<T> | ||
| where | ||
| T: Writeable, | ||
| { | ||
| type Error = Infallible; | ||
| #[inline] | ||
| fn try_write_to<W: fmt::Write + ?Sized>( | ||
| &self, | ||
| sink: &mut W, | ||
| ) -> Result<Result<(), Infallible>, fmt::Error> { | ||
| self.0.write_to(sink).map(Ok) | ||
| } | ||
| #[inline] | ||
| fn try_write_to_parts<S: PartsWrite + ?Sized>( | ||
| &self, | ||
| sink: &mut S, | ||
| ) -> Result<Result<(), Infallible>, fmt::Error> { | ||
| self.0.write_to_parts(sink).map(Ok) | ||
| } | ||
| #[inline] | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| self.0.writeable_length_hint() | ||
| } | ||
| #[inline] | ||
| fn try_write_to_string(&self) -> Result<Cow<str>, (Infallible, Cow<str>)> { | ||
| Ok(self.0.write_to_string()) | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| self.0.writeable_cmp_bytes(other) | ||
| } | ||
| } | ||
| /// Testing macros for types implementing [`TryWriteable`]. | ||
| /// | ||
| /// Arguments, in order: | ||
| /// | ||
| /// 1. The [`TryWriteable`] under test | ||
| /// 2. The expected string value | ||
| /// 3. The expected result value, or `Ok(())` if omitted | ||
| /// 3. [`*_parts_eq`] only: a list of parts (`[(start, end, Part)]`) | ||
| /// | ||
| /// Any remaining arguments get passed to `format!` | ||
| /// | ||
| /// The macros tests the following: | ||
| /// | ||
| /// - Equality of string content | ||
| /// - Equality of parts ([`*_parts_eq`] only) | ||
| /// - Validity of size hint | ||
| /// - Reflexivity of `cmp_bytes` and order against largest and smallest strings | ||
| /// | ||
| /// For a usage example, see [`TryWriteable`]. | ||
| /// | ||
| /// [`*_parts_eq`]: assert_try_writeable_parts_eq | ||
| #[macro_export] | ||
| macro_rules! assert_try_writeable_eq { | ||
| ($actual_writeable:expr, $expected_str:expr $(,)?) => { | ||
| $crate::assert_try_writeable_eq!($actual_writeable, $expected_str, Ok(())) | ||
| }; | ||
| ($actual_writeable:expr, $expected_str:expr, $expected_result:expr $(,)?) => { | ||
| $crate::assert_try_writeable_eq!($actual_writeable, $expected_str, $expected_result, "") | ||
| }; | ||
| ($actual_writeable:expr, $expected_str:expr, $expected_result:expr, $($arg:tt)+) => {{ | ||
| $crate::assert_try_writeable_eq!(@internal, $actual_writeable, $expected_str, $expected_result, $($arg)*); | ||
| }}; | ||
| (@internal, $actual_writeable:expr, $expected_str:expr, $expected_result:expr, $($arg:tt)+) => {{ | ||
| use $crate::TryWriteable; | ||
| let actual_writeable = &$actual_writeable; | ||
| let (actual_str, actual_parts, actual_error) = $crate::_internal::try_writeable_to_parts_for_test(actual_writeable); | ||
| assert_eq!(actual_str, $expected_str, $($arg)*); | ||
| assert_eq!(actual_error, Result::<(), _>::from($expected_result).err(), $($arg)*); | ||
| let actual_result = match actual_writeable.try_write_to_string() { | ||
| Ok(actual_cow_str) => { | ||
| assert_eq!(actual_cow_str, $expected_str, $($arg)+); | ||
| Ok(()) | ||
| } | ||
| Err((e, actual_cow_str)) => { | ||
| assert_eq!(actual_cow_str, $expected_str, $($arg)+); | ||
| Err(e) | ||
| } | ||
| }; | ||
| assert_eq!(actual_result, Result::<(), _>::from($expected_result), $($arg)*); | ||
| let length_hint = actual_writeable.writeable_length_hint(); | ||
| assert!( | ||
| length_hint.0 <= actual_str.len(), | ||
| "hint lower bound {} larger than actual length {}: {}", | ||
| length_hint.0, actual_str.len(), format!($($arg)*), | ||
| ); | ||
| if let Some(upper) = length_hint.1 { | ||
| assert!( | ||
| actual_str.len() <= upper, | ||
| "hint upper bound {} smaller than actual length {}: {}", | ||
| length_hint.0, actual_str.len(), format!($($arg)*), | ||
| ); | ||
| } | ||
| let ordering = actual_writeable.writeable_cmp_bytes($expected_str.as_bytes()); | ||
| assert_eq!(ordering, core::cmp::Ordering::Equal, $($arg)*); | ||
| let ordering = actual_writeable.writeable_cmp_bytes("\u{10FFFF}".as_bytes()); | ||
| assert_eq!(ordering, core::cmp::Ordering::Less, $($arg)*); | ||
| if $expected_str != "" { | ||
| let ordering = actual_writeable.writeable_cmp_bytes("".as_bytes()); | ||
| assert_eq!(ordering, core::cmp::Ordering::Greater, $($arg)*); | ||
| } | ||
| actual_parts // return for assert_try_writeable_parts_eq | ||
| }}; | ||
| } | ||
| /// See [`assert_try_writeable_eq`]. | ||
| #[macro_export] | ||
| macro_rules! assert_try_writeable_parts_eq { | ||
| ($actual_writeable:expr, $expected_str:expr, $expected_parts:expr $(,)?) => { | ||
| $crate::assert_try_writeable_parts_eq!($actual_writeable, $expected_str, Ok(()), $expected_parts) | ||
| }; | ||
| ($actual_writeable:expr, $expected_str:expr, $expected_result:expr, $expected_parts:expr $(,)?) => { | ||
| $crate::assert_try_writeable_parts_eq!($actual_writeable, $expected_str, $expected_result, $expected_parts, "") | ||
| }; | ||
| ($actual_writeable:expr, $expected_str:expr, $expected_result:expr, $expected_parts:expr, $($arg:tt)+) => {{ | ||
| let actual_parts = $crate::assert_try_writeable_eq!(@internal, $actual_writeable, $expected_str, $expected_result, $($arg)*); | ||
| assert_eq!(actual_parts, $expected_parts, $($arg)+); | ||
| }}; | ||
| } | ||
| #[test] | ||
| fn test_result_try_writeable() { | ||
| let mut result: Result<&str, usize> = Ok("success"); | ||
| assert_try_writeable_eq!(result, "success"); | ||
| result = Err(44); | ||
| assert_try_writeable_eq!(result, "44", Err(44)); | ||
| assert_try_writeable_parts_eq!(result, "44", Err(44), [(0, 2, Part::ERROR)]) | ||
| } |
| // 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 ). | ||
| #[allow(dead_code)] | ||
| pub const KEBAB_CASE_STRINGS: &[&str] = &[ | ||
| "ca", | ||
| "ca-ÉS", | ||
| "ca-ÉS-u-ca-buddhist", | ||
| "ca-ÉS-valencia", | ||
| "ca-ÉS-x-gbp", | ||
| "ca-ÉS-x-gbp-short", | ||
| "ca-ÉS-x-usd", | ||
| "ca-ÉS-xyzabc", | ||
| "ca-x-eur", | ||
| "cat", | ||
| "cat-bus", | ||
| "cat-🚐", | ||
| "pl-Latn-PL", | ||
| "und", | ||
| "und-fonipa", | ||
| "und-u-ca-hebrew", | ||
| "und-u-ca-japanese", | ||
| "und-x-mxn", | ||
| "zh", | ||
| ]; |
| { | ||
| "git": { | ||
| "sha1": "8b080b3753b91c58113c47c99108185d0614499c" | ||
| "sha1": "55cd12ebb25c6261492e1e3dfa2e6453c54dde31" | ||
| }, | ||
| "path_in_vcs": "utils/writeable" | ||
| } |
+52
-0
@@ -38,2 +38,32 @@ // This file is part of ICU4X. For terms of use, please see the file | ||
| /// A sample type that contains multiple fields | ||
| struct ComplexWriteable<'a> { | ||
| prefix: &'a str, | ||
| n0: usize, | ||
| infix: &'a str, | ||
| n1: usize, | ||
| suffix: &'a str, | ||
| } | ||
| impl Writeable for ComplexWriteable<'_> { | ||
| fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { | ||
| self.prefix.write_to(sink)?; | ||
| self.n0.write_to(sink)?; | ||
| self.infix.write_to(sink)?; | ||
| self.n1.write_to(sink)?; | ||
| self.suffix.write_to(sink)?; | ||
| Ok(()) | ||
| } | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| self.prefix.writeable_length_hint() | ||
| + self.n0.writeable_length_hint() | ||
| + self.infix.writeable_length_hint() | ||
| + self.n1.writeable_length_hint() | ||
| + self.suffix.writeable_length_hint() | ||
| } | ||
| } | ||
| writeable::impl_display_with_writeable!(ComplexWriteable<'_>); | ||
| const SHORT_STR: &str = "short"; | ||
@@ -67,2 +97,3 @@ const MEDIUM_STR: &str = "this is a medium-length string"; | ||
| display_benches(c); | ||
| complex_benches(c); | ||
| } | ||
@@ -163,3 +194,24 @@ } | ||
| #[cfg(feature = "bench")] | ||
| fn complex_benches(c: &mut Criterion) { | ||
| const COMPLEX_WRITEABLE_MEDIUM: ComplexWriteable = ComplexWriteable { | ||
| prefix: "There are ", | ||
| n0: 55, | ||
| infix: " apples and ", | ||
| n1: 8124, | ||
| suffix: " oranges", | ||
| }; | ||
| c.bench_function("complex/write_to_string/medium", |b| { | ||
| b.iter(|| { | ||
| black_box(COMPLEX_WRITEABLE_MEDIUM) | ||
| .write_to_string() | ||
| .into_owned() | ||
| }); | ||
| }); | ||
| c.bench_function("complex/display_to_string/medium", |b| { | ||
| b.iter(|| black_box(COMPLEX_WRITEABLE_MEDIUM).to_string()); | ||
| }); | ||
| } | ||
| criterion_group!(benches, overview_bench,); | ||
| criterion_main!(benches); |
+207
-104
@@ -6,11 +6,2 @@ # This file is automatically @generated by Cargo. | ||
| [[package]] | ||
| name = "aho-corasick" | ||
| version = "1.1.1" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" | ||
| dependencies = [ | ||
| "memchr", | ||
| ] | ||
| [[package]] | ||
| name = "anes" | ||
@@ -22,13 +13,2 @@ version = "0.1.6" | ||
| [[package]] | ||
| name = "atty" | ||
| version = "0.2.14" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" | ||
| dependencies = [ | ||
| "hermit-abi", | ||
| "libc", | ||
| "winapi", | ||
| ] | ||
| [[package]] | ||
| name = "autocfg" | ||
@@ -46,2 +26,8 @@ version = "1.1.0" | ||
| [[package]] | ||
| name = "bitflags" | ||
| version = "2.4.2" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" | ||
| [[package]] | ||
| name = "bumpalo" | ||
@@ -93,29 +79,32 @@ version = "3.14.0" | ||
| name = "clap" | ||
| version = "3.2.25" | ||
| version = "4.2.1" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" | ||
| checksum = "046ae530c528f252094e4a77886ee1374437744b2bff1497aa898bbddbbb29b3" | ||
| dependencies = [ | ||
| "bitflags", | ||
| "clap_lex", | ||
| "indexmap", | ||
| "textwrap", | ||
| "clap_builder", | ||
| ] | ||
| [[package]] | ||
| name = "clap_lex" | ||
| version = "0.2.4" | ||
| name = "clap_builder" | ||
| version = "4.2.1" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" | ||
| checksum = "223163f58c9a40c3b0a43e1c4b50a9ce09f007ea2cb1ec258a687945b4b7929f" | ||
| dependencies = [ | ||
| "os_str_bytes", | ||
| "bitflags 1.3.2", | ||
| "clap_lex", | ||
| ] | ||
| [[package]] | ||
| name = "clap_lex" | ||
| version = "0.4.1" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" | ||
| [[package]] | ||
| name = "criterion" | ||
| version = "0.4.0" | ||
| version = "0.5.1" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" | ||
| checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" | ||
| dependencies = [ | ||
| "anes", | ||
| "atty", | ||
| "cast", | ||
@@ -125,5 +114,6 @@ "ciborium", | ||
| "criterion-plot", | ||
| "is-terminal", | ||
| "itertools", | ||
| "lazy_static", | ||
| "num-traits", | ||
| "once_cell", | ||
| "oorandom", | ||
@@ -176,8 +166,5 @@ "plotters", | ||
| name = "crossbeam-utils" | ||
| version = "0.8.16" | ||
| version = "0.8.19" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" | ||
| dependencies = [ | ||
| "cfg-if", | ||
| ] | ||
| checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" | ||
@@ -191,2 +178,12 @@ [[package]] | ||
| [[package]] | ||
| name = "errno" | ||
| version = "0.3.8" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" | ||
| dependencies = [ | ||
| "libc", | ||
| "windows-sys 0.52.0", | ||
| ] | ||
| [[package]] | ||
| name = "getrandom" | ||
@@ -209,24 +206,16 @@ version = "0.2.10" | ||
| [[package]] | ||
| name = "hashbrown" | ||
| version = "0.12.3" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" | ||
| [[package]] | ||
| name = "hermit-abi" | ||
| version = "0.1.19" | ||
| version = "0.3.3" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" | ||
| dependencies = [ | ||
| "libc", | ||
| ] | ||
| checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" | ||
| [[package]] | ||
| name = "indexmap" | ||
| version = "1.9.3" | ||
| name = "is-terminal" | ||
| version = "0.4.9" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" | ||
| checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" | ||
| dependencies = [ | ||
| "autocfg", | ||
| "hashbrown", | ||
| "hermit-abi", | ||
| "rustix", | ||
| "windows-sys 0.48.0", | ||
| ] | ||
@@ -259,12 +248,12 @@ | ||
| [[package]] | ||
| name = "lazy_static" | ||
| version = "1.4.0" | ||
| name = "libc" | ||
| version = "0.2.153" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" | ||
| checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" | ||
| [[package]] | ||
| name = "libc" | ||
| version = "0.2.148" | ||
| name = "linux-raw-sys" | ||
| version = "0.4.13" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" | ||
| checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" | ||
@@ -278,8 +267,2 @@ [[package]] | ||
| [[package]] | ||
| name = "memchr" | ||
| version = "2.6.3" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" | ||
| [[package]] | ||
| name = "memoffset" | ||
@@ -295,5 +278,5 @@ version = "0.9.0" | ||
| name = "num-traits" | ||
| version = "0.2.16" | ||
| version = "0.2.18" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" | ||
| checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" | ||
| dependencies = [ | ||
@@ -316,8 +299,2 @@ "autocfg", | ||
| [[package]] | ||
| name = "os_str_bytes" | ||
| version = "6.5.1" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "4d5d9eb14b174ee9aa2ef96dc2b94637a2d4b6e7cb873c7e171f0c20c6cf3eac" | ||
| [[package]] | ||
| name = "plotters" | ||
@@ -358,5 +335,5 @@ version = "0.3.5" | ||
| name = "proc-macro2" | ||
| version = "1.0.67" | ||
| version = "1.0.82" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" | ||
| checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b" | ||
| dependencies = [ | ||
@@ -368,5 +345,5 @@ "unicode-ident", | ||
| name = "quote" | ||
| version = "1.0.33" | ||
| version = "1.0.35" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" | ||
| checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" | ||
| dependencies = [ | ||
@@ -428,9 +405,6 @@ "proc-macro2", | ||
| name = "regex" | ||
| version = "1.9.5" | ||
| version = "1.8.4" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" | ||
| checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f" | ||
| dependencies = [ | ||
| "aho-corasick", | ||
| "memchr", | ||
| "regex-automata", | ||
| "regex-syntax", | ||
@@ -440,13 +414,2 @@ ] | ||
| [[package]] | ||
| name = "regex-automata" | ||
| version = "0.3.8" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" | ||
| dependencies = [ | ||
| "aho-corasick", | ||
| "memchr", | ||
| "regex-syntax", | ||
| ] | ||
| [[package]] | ||
| name = "regex-syntax" | ||
@@ -458,2 +421,15 @@ version = "0.7.5" | ||
| [[package]] | ||
| name = "rustix" | ||
| version = "0.38.31" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" | ||
| dependencies = [ | ||
| "bitflags 2.4.2", | ||
| "errno", | ||
| "libc", | ||
| "linux-raw-sys", | ||
| "windows-sys 0.52.0", | ||
| ] | ||
| [[package]] | ||
| name = "ryu" | ||
@@ -512,5 +488,5 @@ version = "1.0.15" | ||
| name = "syn" | ||
| version = "2.0.37" | ||
| version = "2.0.58" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" | ||
| checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" | ||
| dependencies = [ | ||
@@ -523,8 +499,2 @@ "proc-macro2", | ||
| [[package]] | ||
| name = "textwrap" | ||
| version = "0.16.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" | ||
| [[package]] | ||
| name = "tinytemplate" | ||
@@ -657,7 +627,140 @@ version = "1.2.1" | ||
| [[package]] | ||
| name = "windows-sys" | ||
| version = "0.48.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" | ||
| dependencies = [ | ||
| "windows-targets 0.48.5", | ||
| ] | ||
| [[package]] | ||
| name = "windows-sys" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" | ||
| dependencies = [ | ||
| "windows-targets 0.52.0", | ||
| ] | ||
| [[package]] | ||
| name = "windows-targets" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" | ||
| dependencies = [ | ||
| "windows_aarch64_gnullvm 0.48.5", | ||
| "windows_aarch64_msvc 0.48.5", | ||
| "windows_i686_gnu 0.48.5", | ||
| "windows_i686_msvc 0.48.5", | ||
| "windows_x86_64_gnu 0.48.5", | ||
| "windows_x86_64_gnullvm 0.48.5", | ||
| "windows_x86_64_msvc 0.48.5", | ||
| ] | ||
| [[package]] | ||
| name = "windows-targets" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" | ||
| dependencies = [ | ||
| "windows_aarch64_gnullvm 0.52.0", | ||
| "windows_aarch64_msvc 0.52.0", | ||
| "windows_i686_gnu 0.52.0", | ||
| "windows_i686_msvc 0.52.0", | ||
| "windows_x86_64_gnu 0.52.0", | ||
| "windows_x86_64_gnullvm 0.52.0", | ||
| "windows_x86_64_msvc 0.52.0", | ||
| ] | ||
| [[package]] | ||
| name = "windows_aarch64_gnullvm" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" | ||
| [[package]] | ||
| name = "windows_aarch64_gnullvm" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" | ||
| [[package]] | ||
| name = "windows_aarch64_msvc" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" | ||
| [[package]] | ||
| name = "windows_aarch64_msvc" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" | ||
| [[package]] | ||
| name = "windows_i686_gnu" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" | ||
| [[package]] | ||
| name = "windows_i686_gnu" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" | ||
| [[package]] | ||
| name = "windows_i686_msvc" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" | ||
| [[package]] | ||
| name = "windows_i686_msvc" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" | ||
| [[package]] | ||
| name = "windows_x86_64_gnu" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" | ||
| [[package]] | ||
| name = "windows_x86_64_gnu" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" | ||
| [[package]] | ||
| name = "windows_x86_64_gnullvm" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" | ||
| [[package]] | ||
| name = "windows_x86_64_gnullvm" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" | ||
| [[package]] | ||
| name = "windows_x86_64_msvc" | ||
| version = "0.48.5" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" | ||
| [[package]] | ||
| name = "windows_x86_64_msvc" | ||
| version = "0.52.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" | ||
| [[package]] | ||
| name = "writeable" | ||
| version = "0.5.4" | ||
| version = "0.5.5" | ||
| dependencies = [ | ||
| "criterion", | ||
| "either", | ||
| "rand", | ||
| ] |
+9
-3
@@ -16,3 +16,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "writeable" | ||
| version = "0.5.4" | ||
| version = "0.5.5" | ||
| authors = ["The ICU4X Project Developers"] | ||
@@ -31,3 +31,3 @@ include = [ | ||
| readme = "README.md" | ||
| license-file = "LICENSE" | ||
| license = "Unicode-3.0" | ||
| repository = "https://github.com/unicode-org/icu4x" | ||
@@ -51,2 +51,7 @@ | ||
| [dependencies.either] | ||
| version = "1.9.0" | ||
| optional = true | ||
| default-features = false | ||
| [dev-dependencies.rand] | ||
@@ -58,4 +63,5 @@ version = "0.8" | ||
| bench = [] | ||
| either = ["dep:either"] | ||
| [target."cfg(not(target_arch = \"wasm32\"))".dev-dependencies.criterion] | ||
| version = "0.4" | ||
| version = "0.5.0" |
@@ -50,3 +50,4 @@ // This file is part of ICU4X. For terms of use, please see the file | ||
| let (string, parts) = writeable_to_parts_for_test(&WriteableMessage("world")).unwrap(); | ||
| let (string, parts) = | ||
| writeable::_internal::writeable_to_parts_for_test(&WriteableMessage("world")); | ||
@@ -53,0 +54,0 @@ assert_eq!(string, "Hello world 😅"); |
+3
-1
@@ -5,3 +5,3 @@ UNICODE LICENSE V3 | ||
| Copyright © 2020-2023 Unicode, Inc. | ||
| Copyright © 2020-2024 Unicode, Inc. | ||
@@ -42,2 +42,4 @@ NOTICE TO USER: Carefully read the following legal agreement. BY | ||
| SPDX-License-Identifier: Unicode-3.0 | ||
| — | ||
@@ -44,0 +46,0 @@ |
+109
-1
@@ -6,3 +6,2 @@ // This file is part of ICU4X. For terms of use, please see the file | ||
| use crate::*; | ||
| use alloc::borrow::Cow; | ||
| use core::fmt; | ||
@@ -120,2 +119,7 @@ | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| self.as_bytes().cmp(other) | ||
| } | ||
| } | ||
@@ -138,4 +142,33 @@ | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| self.as_bytes().cmp(other) | ||
| } | ||
| } | ||
| impl Writeable for char { | ||
| #[inline] | ||
| fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { | ||
| sink.write_char(*self) | ||
| } | ||
| #[inline] | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| LengthHint::exact(self.len_utf8()) | ||
| } | ||
| #[inline] | ||
| fn write_to_string(&self) -> Cow<str> { | ||
| let mut s = String::with_capacity(self.len_utf8()); | ||
| s.push(*self); | ||
| Cow::Owned(s) | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| self.encode_utf8(&mut [0u8; 4]).as_bytes().cmp(other) | ||
| } | ||
| } | ||
| impl<T: Writeable + ?Sized> Writeable for &T { | ||
@@ -161,4 +194,45 @@ #[inline] | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| (*self).writeable_cmp_bytes(other) | ||
| } | ||
| } | ||
| macro_rules! impl_write_smart_pointer { | ||
| ($ty:path, T: $extra_bound:path) => { | ||
| impl<'a, T: ?Sized + Writeable + $extra_bound> Writeable for $ty { | ||
| #[inline] | ||
| fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { | ||
| core::borrow::Borrow::<T>::borrow(self).write_to(sink) | ||
| } | ||
| #[inline] | ||
| fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> fmt::Result { | ||
| core::borrow::Borrow::<T>::borrow(self).write_to_parts(sink) | ||
| } | ||
| #[inline] | ||
| fn writeable_length_hint(&self) -> LengthHint { | ||
| core::borrow::Borrow::<T>::borrow(self).writeable_length_hint() | ||
| } | ||
| #[inline] | ||
| fn write_to_string(&self) -> Cow<str> { | ||
| core::borrow::Borrow::<T>::borrow(self).write_to_string() | ||
| } | ||
| #[inline] | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| core::borrow::Borrow::<T>::borrow(self).writeable_cmp_bytes(other) | ||
| } | ||
| } | ||
| }; | ||
| ($ty:path) => { | ||
| // Add a harmless duplicate Writeable bound | ||
| impl_write_smart_pointer!($ty, T: Writeable); | ||
| }; | ||
| } | ||
| impl_write_smart_pointer!(Cow<'a, T>, T: alloc::borrow::ToOwned); | ||
| impl_write_smart_pointer!(alloc::boxed::Box<T>); | ||
| impl_write_smart_pointer!(alloc::rc::Rc<T>); | ||
| impl_write_smart_pointer!(alloc::sync::Arc<T>); | ||
| #[test] | ||
@@ -169,2 +243,4 @@ fn test_string_impls() { | ||
| assert_writeable_eq!(&writeables[1], "abc"); | ||
| assert!(matches!(writeables[0].write_to_string(), Cow::Borrowed(_))); | ||
| assert!(matches!(writeables[1].write_to_string(), Cow::Borrowed(_))); | ||
| } | ||
@@ -180,2 +256,34 @@ | ||
| // test char impl | ||
| let chars = ['a', 'β', '你', '😀']; | ||
| for i in 0..chars.len() { | ||
| let s = String::from(chars[i]); | ||
| assert_writeable_eq!(&chars[i], s); | ||
| for j in 0..chars.len() { | ||
| assert_eq!( | ||
| chars[j].writeable_cmp_bytes(s.as_bytes()), | ||
| chars[j].cmp(&chars[i]), | ||
| "{:?} vs {:?}", | ||
| chars[j], | ||
| chars[i] | ||
| ); | ||
| } | ||
| } | ||
| // test Cow impl | ||
| let arr: &[Cow<str>] = &[Cow::Borrowed(""), Cow::Owned("abc".to_string())]; | ||
| check_writeable_slice(arr); | ||
| // test Box impl | ||
| let arr: &[Box<str>] = &["".into(), "abc".into()]; | ||
| check_writeable_slice(arr); | ||
| // test Rc impl | ||
| let arr: &[alloc::rc::Rc<str>] = &["".into(), "abc".into()]; | ||
| check_writeable_slice(arr); | ||
| // test Arc impl | ||
| let arr: &[alloc::sync::Arc<str>] = &["".into(), "abc".into()]; | ||
| check_writeable_slice(arr); | ||
| // test &T impl | ||
@@ -182,0 +290,0 @@ let arr: &[&String] = &[&String::new(), &"abc".to_owned()]; |
+121
-99
@@ -5,3 +5,3 @@ // This file is part of ICU4X. For terms of use, please see the file | ||
| // https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations | ||
| // https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations | ||
| #![cfg_attr(all(not(test), not(doc)), no_std)] | ||
@@ -70,10 +70,32 @@ #![cfg_attr( | ||
| mod cmp; | ||
| #[cfg(feature = "either")] | ||
| mod either; | ||
| mod impls; | ||
| mod ops; | ||
| mod parts_write_adapter; | ||
| mod testing; | ||
| mod try_writeable; | ||
| use alloc::borrow::Cow; | ||
| use alloc::string::String; | ||
| use alloc::vec::Vec; | ||
| use core::fmt; | ||
| pub use try_writeable::TryWriteable; | ||
| /// Helper types for trait impls. | ||
| pub mod adapters { | ||
| use super::*; | ||
| pub use parts_write_adapter::CoreWriteAsPartsWrite; | ||
| pub use try_writeable::TryWriteableInfallibleAsWriteable; | ||
| pub use try_writeable::WriteableAsTryWriteableInfallible; | ||
| } | ||
| #[doc(hidden)] | ||
| pub mod _internal { | ||
| pub use super::testing::try_writeable_to_parts_for_test; | ||
| pub use super::testing::writeable_to_parts_for_test; | ||
| } | ||
| /// A hint to help consumers of `Writeable` pre-allocate bytes before they call | ||
@@ -170,2 +192,12 @@ /// [`write_to`](Writeable::write_to). | ||
| impl Part { | ||
| /// A part that should annotate error segments in [`TryWriteable`] output. | ||
| /// | ||
| /// For an example, see [`TryWriteable`]. | ||
| pub const ERROR: Part = Part { | ||
| category: "writeable", | ||
| value: "error", | ||
| }; | ||
| } | ||
| /// A sink that supports annotating parts of the string with `Part`s. | ||
@@ -188,26 +220,3 @@ pub trait PartsWrite: fmt::Write { | ||
| 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)) | ||
| self.write_to_parts(&mut parts_write_adapter::CoreWriteAsPartsWrite(sink)) | ||
| } | ||
@@ -277,2 +286,47 @@ | ||
| } | ||
| /// Compares the contents of this `Writeable` to the given bytes | ||
| /// without allocating a String to hold the `Writeable` contents. | ||
| /// | ||
| /// This returns a lexicographical comparison, the same as if the Writeable | ||
| /// were first converted to a String and then compared with `Ord`. For a | ||
| /// locale-sensitive string ordering, use an ICU4X Collator. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use core::cmp::Ordering; | ||
| /// use core::fmt; | ||
| /// use writeable::Writeable; | ||
| /// | ||
| /// struct WelcomeMessage<'s> { | ||
| /// pub name: &'s str, | ||
| /// } | ||
| /// | ||
| /// impl<'s> Writeable for WelcomeMessage<'s> { | ||
| /// // see impl in Writeable docs | ||
| /// # 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(()) | ||
| /// # } | ||
| /// } | ||
| /// | ||
| /// let message = WelcomeMessage { name: "Alice" }; | ||
| /// let message_str = message.write_to_string(); | ||
| /// | ||
| /// assert_eq!(Ordering::Equal, message.writeable_cmp_bytes(b"Hello, Alice!")); | ||
| /// | ||
| /// assert_eq!(Ordering::Greater, message.writeable_cmp_bytes(b"Alice!")); | ||
| /// assert_eq!(Ordering::Greater, (*message_str).cmp("Alice!")); | ||
| /// | ||
| /// assert_eq!(Ordering::Less, message.writeable_cmp_bytes(b"Hello, Bob!")); | ||
| /// assert_eq!(Ordering::Less, (*message_str).cmp("Hello, Bob!")); | ||
| /// ``` | ||
| fn writeable_cmp_bytes(&self, other: &[u8]) -> core::cmp::Ordering { | ||
| let mut wc = cmp::WriteComparator::new(other); | ||
| let _ = self.write_to(&mut wc); | ||
| wc.finish().reverse() | ||
| } | ||
| } | ||
@@ -299,9 +353,19 @@ | ||
| /// 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)]`). | ||
| /// Testing macros for types implementing [`Writeable`]. | ||
| /// | ||
| /// The macros tests for equality of string content, parts (*_parts_eq only), and | ||
| /// verify the size hint. | ||
| /// Arguments, in order: | ||
| /// | ||
| /// 1. The [`Writeable`] under test | ||
| /// 2. The expected string value | ||
| /// 3. [`*_parts_eq`] only: a list of parts (`[(start, end, Part)]`) | ||
| /// | ||
| /// Any remaining arguments get passed to `format!` | ||
| /// | ||
| /// The macros tests the following: | ||
| /// | ||
| /// - Equality of string content | ||
| /// - Equality of parts ([`*_parts_eq`] only) | ||
| /// - Validity of size hint | ||
| /// - Reflexivity of `cmp_bytes` and order against largest and smallest strings | ||
| /// | ||
| /// # Examples | ||
@@ -349,26 +413,42 @@ /// | ||
| /// ``` | ||
| /// | ||
| /// [`*_parts_eq`]: assert_writeable_parts_eq | ||
| #[macro_export] | ||
| macro_rules! assert_writeable_eq { | ||
| ($actual_writeable:expr, $expected_str:expr $(,)?) => { | ||
| $crate::assert_writeable_eq!($actual_writeable, $expected_str, ""); | ||
| $crate::assert_writeable_eq!($actual_writeable, $expected_str, "") | ||
| }; | ||
| ($actual_writeable:expr, $expected_str:expr, $($arg:tt)+) => {{ | ||
| $crate::assert_writeable_eq!(@internal, $actual_writeable, $expected_str, $($arg)*); | ||
| }}; | ||
| (@internal, $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(); | ||
| let (actual_str, actual_parts) = $crate::_internal::writeable_to_parts_for_test(actual_writeable); | ||
| let actual_len = actual_str.len(); | ||
| assert_eq!(actual_str, $expected_str, $($arg)*); | ||
| assert_eq!(actual_str, $crate::Writeable::write_to_string(actual_writeable), $($arg)+); | ||
| let length_hint = $crate::Writeable::writeable_length_hint(actual_writeable); | ||
| let lower = length_hint.0; | ||
| assert!( | ||
| length_hint.0 <= actual_str.len(), | ||
| "hint lower bound {} larger than actual length {}: {}", | ||
| length_hint.0, actual_str.len(), format!($($arg)*), | ||
| lower <= actual_len, | ||
| "hint lower bound {lower} larger than actual length {actual_len}: {}", | ||
| format!($($arg)*), | ||
| ); | ||
| if let Some(upper) = length_hint.1 { | ||
| assert!( | ||
| actual_str.len() <= upper, | ||
| "hint upper bound {} smaller than actual length {}: {}", | ||
| length_hint.0, actual_str.len(), format!($($arg)*), | ||
| actual_len <= upper, | ||
| "hint upper bound {upper} smaller than actual length {actual_len}: {}", | ||
| format!($($arg)*), | ||
| ); | ||
| } | ||
| assert_eq!(actual_writeable.to_string(), $expected_str); | ||
| let ordering = $crate::Writeable::writeable_cmp_bytes(actual_writeable, $expected_str.as_bytes()); | ||
| assert_eq!(ordering, core::cmp::Ordering::Equal, $($arg)*); | ||
| let ordering = $crate::Writeable::writeable_cmp_bytes(actual_writeable, "\u{10FFFF}".as_bytes()); | ||
| assert_eq!(ordering, core::cmp::Ordering::Less, $($arg)*); | ||
| if $expected_str != "" { | ||
| let ordering = $crate::Writeable::writeable_cmp_bytes(actual_writeable, "".as_bytes()); | ||
| assert_eq!(ordering, core::cmp::Ordering::Greater, $($arg)*); | ||
| } | ||
| actual_parts // return for assert_writeable_parts_eq | ||
| }}; | ||
@@ -381,66 +461,8 @@ } | ||
| ($actual_writeable:expr, $expected_str:expr, $expected_parts:expr $(,)?) => { | ||
| $crate::assert_writeable_parts_eq!($actual_writeable, $expected_str, $expected_parts, ""); | ||
| $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)+); | ||
| let actual_parts = $crate::assert_writeable_eq!(@internal, $actual_writeable, $expected_str, $($arg)*); | ||
| assert_eq!(actual_parts, $expected_parts, $($arg)+); | ||
| let length_hint = $crate::Writeable::writeable_length_hint(actual_writeable); | ||
| assert!(length_hint.0 <= actual_str.len(), $($arg)+); | ||
| if let Some(upper) = length_hint.1 { | ||
| assert!(actual_str.len() <= upper, $($arg)+); | ||
| } | ||
| assert_eq!(actual_writeable.to_string(), $expected_str); | ||
| }}; | ||
| } | ||
| #[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)?; | ||
| let end = self.string.len(); | ||
| if start < end { | ||
| self.parts.push((start, end, 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)) | ||
| } |
Sorry, the diff of this file is not supported yet