Skip to main content

writeable/
impls.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use crate::*;
6use core::fmt;
7
8macro_rules! impl_write_num {
9    // random_call exists since usize doesn't have a rand impl. Should always be `random` or empty
10    ($u:ty, $i:ty, $test:ident $(,$random_call:ident)?) => {
11        impl $crate::Writeable for $u {
12            fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
13                const MAX_LEN: usize = <$u>::MAX.ilog10() as usize + 1;
14                let mut buf = [b'0'; MAX_LEN];
15                let mut n = *self;
16                let mut i = MAX_LEN;
17                #[expect(clippy::indexing_slicing)] // n < 10^i
18                #[allow(trivial_numeric_casts)]
19                while n != 0 {
20                    i -= 1;
21                    buf[i] = b'0' + (n % 10) as u8;
22                    n /= 10;
23                }
24                if i == MAX_LEN {
25                    debug_assert_eq!(*self, 0);
26                    i -= 1;
27                }
28                #[expect(clippy::indexing_slicing)] // buf is ASCII
29                let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
30                sink.write_str(s)
31            }
32
33            fn writeable_length_hint(&self) -> $crate::LengthHint {
34                LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
35            }
36        }
37
38        impl $crate::Writeable for $i {
39            fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
40                if self.is_negative() {
41                    sink.write_str("-")?;
42                }
43                self.unsigned_abs().write_to(sink)
44            }
45
46            fn writeable_length_hint(&self) -> $crate::LengthHint {
47                $crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 })
48                    + self.unsigned_abs().writeable_length_hint()
49            }
50        }
51
52        #[test]
53        #[allow(trivial_numeric_casts)]
54        fn $test() {
55            use $crate::assert_writeable_eq;
56            assert_writeable_eq!(&(0 as $u), "0");
57            assert_writeable_eq!(&(0 as $i), "0");
58            assert_writeable_eq!(&(-0 as $i), "0");
59            assert_writeable_eq!(&(1 as $u), "1");
60            assert_writeable_eq!(&(1 as $i), "1");
61            assert_writeable_eq!(&(-1 as $i), "-1");
62            assert_writeable_eq!(&(9 as $u), "9");
63            assert_writeable_eq!(&(9 as $i), "9");
64            assert_writeable_eq!(&(-9 as $i), "-9");
65            assert_writeable_eq!(&(10 as $u), "10");
66            assert_writeable_eq!(&(10 as $i), "10");
67            assert_writeable_eq!(&(-10 as $i), "-10");
68            assert_writeable_eq!(&(99 as $u), "99");
69            assert_writeable_eq!(&(99 as $i), "99");
70            assert_writeable_eq!(&(-99 as $i), "-99");
71            assert_writeable_eq!(&(100 as $u), "100");
72            assert_writeable_eq!(&(-100 as $i), "-100");
73            assert_writeable_eq!(&<$u>::MAX, <$u>::MAX.to_string());
74            assert_writeable_eq!(&<$i>::MAX, <$i>::MAX.to_string());
75            assert_writeable_eq!(&<$i>::MIN, <$i>::MIN.to_string());
76
77            $(
78
79                use rand::{rngs::SmallRng, Rng, SeedableRng};
80                let mut rng = SmallRng::seed_from_u64(4); // chosen by fair dice roll.
81                                                          // guaranteed to be random.
82                for _ in 0..1000 {
83                    let rand = rng.$random_call::<$u>();
84                    assert_writeable_eq!(rand, rand.to_string());
85                }
86            )?
87        }
88    };
89}
90
91impl crate::Writeable for u8 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        const MAX_LEN: usize = <u8>::MAX.ilog10() as usize + 1;
        let mut buf = [b'0'; MAX_LEN];
        let mut n = *self;
        let mut i = MAX_LEN;

        #[expect(clippy :: indexing_slicing)]
        #[allow(trivial_numeric_casts)]
        while n != 0 { i -= 1; buf[i] = b'0' + (n % 10) as u8; n /= 10; }
        if i == MAX_LEN {
            if true {
                {
                    match (&*self, &0) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            i -= 1;
        }
        #[expect(clippy :: indexing_slicing)]
        let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
        sink.write_str(s)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
    }
}
impl crate::Writeable for i8 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        if self.is_negative() { sink.write_str("-")?; }
        self.unsigned_abs().write_to(sink)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 }) +
            self.unsigned_abs().writeable_length_hint()
    }
}impl_write_num!(u8, i8, test_u8, random);
92impl crate::Writeable for u16 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        const MAX_LEN: usize = <u16>::MAX.ilog10() as usize + 1;
        let mut buf = [b'0'; MAX_LEN];
        let mut n = *self;
        let mut i = MAX_LEN;

        #[expect(clippy :: indexing_slicing)]
        #[allow(trivial_numeric_casts)]
        while n != 0 { i -= 1; buf[i] = b'0' + (n % 10) as u8; n /= 10; }
        if i == MAX_LEN {
            if true {
                {
                    match (&*self, &0) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            i -= 1;
        }
        #[expect(clippy :: indexing_slicing)]
        let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
        sink.write_str(s)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
    }
}
impl crate::Writeable for i16 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        if self.is_negative() { sink.write_str("-")?; }
        self.unsigned_abs().write_to(sink)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 }) +
            self.unsigned_abs().writeable_length_hint()
    }
}impl_write_num!(u16, i16, test_u16, random);
93impl crate::Writeable for u32 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        const MAX_LEN: usize = <u32>::MAX.ilog10() as usize + 1;
        let mut buf = [b'0'; MAX_LEN];
        let mut n = *self;
        let mut i = MAX_LEN;

        #[expect(clippy :: indexing_slicing)]
        #[allow(trivial_numeric_casts)]
        while n != 0 { i -= 1; buf[i] = b'0' + (n % 10) as u8; n /= 10; }
        if i == MAX_LEN {
            if true {
                {
                    match (&*self, &0) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            i -= 1;
        }
        #[expect(clippy :: indexing_slicing)]
        let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
        sink.write_str(s)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
    }
}
impl crate::Writeable for i32 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        if self.is_negative() { sink.write_str("-")?; }
        self.unsigned_abs().write_to(sink)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 }) +
            self.unsigned_abs().writeable_length_hint()
    }
}impl_write_num!(u32, i32, test_u32, random);
94impl crate::Writeable for u64 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        const MAX_LEN: usize = <u64>::MAX.ilog10() as usize + 1;
        let mut buf = [b'0'; MAX_LEN];
        let mut n = *self;
        let mut i = MAX_LEN;

        #[expect(clippy :: indexing_slicing)]
        #[allow(trivial_numeric_casts)]
        while n != 0 { i -= 1; buf[i] = b'0' + (n % 10) as u8; n /= 10; }
        if i == MAX_LEN {
            if true {
                {
                    match (&*self, &0) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            i -= 1;
        }
        #[expect(clippy :: indexing_slicing)]
        let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
        sink.write_str(s)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
    }
}
impl crate::Writeable for i64 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        if self.is_negative() { sink.write_str("-")?; }
        self.unsigned_abs().write_to(sink)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 }) +
            self.unsigned_abs().writeable_length_hint()
    }
}impl_write_num!(u64, i64, test_u64, random);
95impl crate::Writeable for u128 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        const MAX_LEN: usize = <u128>::MAX.ilog10() as usize + 1;
        let mut buf = [b'0'; MAX_LEN];
        let mut n = *self;
        let mut i = MAX_LEN;

        #[expect(clippy :: indexing_slicing)]
        #[allow(trivial_numeric_casts)]
        while n != 0 { i -= 1; buf[i] = b'0' + (n % 10) as u8; n /= 10; }
        if i == MAX_LEN {
            if true {
                {
                    match (&*self, &0) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            i -= 1;
        }
        #[expect(clippy :: indexing_slicing)]
        let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
        sink.write_str(s)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
    }
}
impl crate::Writeable for i128 {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        if self.is_negative() { sink.write_str("-")?; }
        self.unsigned_abs().write_to(sink)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 }) +
            self.unsigned_abs().writeable_length_hint()
    }
}impl_write_num!(u128, i128, test_u128, random);
96impl crate::Writeable for usize {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        const MAX_LEN: usize = <usize>::MAX.ilog10() as usize + 1;
        let mut buf = [b'0'; MAX_LEN];
        let mut n = *self;
        let mut i = MAX_LEN;

        #[expect(clippy :: indexing_slicing)]
        #[allow(trivial_numeric_casts)]
        while n != 0 { i -= 1; buf[i] = b'0' + (n % 10) as u8; n /= 10; }
        if i == MAX_LEN {
            if true {
                {
                    match (&*self, &0) {
                        (left_val, right_val) => {
                            if !(*left_val == *right_val) {
                                let kind = ::core::panicking::AssertKind::Eq;
                                ::core::panicking::assert_failed(kind, &*left_val,
                                    &*right_val, ::core::option::Option::None);
                            }
                        }
                    }
                };
            };
            i -= 1;
        }
        #[expect(clippy :: indexing_slicing)]
        let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
        sink.write_str(s)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
    }
}
impl crate::Writeable for isize {
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        if self.is_negative() { sink.write_str("-")?; }
        self.unsigned_abs().write_to(sink)
    }
    fn writeable_length_hint(&self) -> crate::LengthHint {
        crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 }) +
            self.unsigned_abs().writeable_length_hint()
    }
}impl_write_num!(usize, isize, test_usize);
97
98impl Writeable for str {
99    #[inline]
100    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
101        sink.write_str(self)
102    }
103
104    #[inline]
105    fn writeable_length_hint(&self) -> LengthHint {
106        LengthHint::exact(self.len())
107    }
108
109    #[inline]
110    fn writeable_borrow(&self) -> Option<&str> {
111        Some(self)
112    }
113}
114
115#[cfg(feature = "alloc")]
116crate::impl_writeable_delegate!(String, |&self| self.as_str());
117
118impl Writeable for char {
119    #[inline]
120    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
121        sink.write_char(*self)
122    }
123
124    #[inline]
125    fn writeable_length_hint(&self) -> LengthHint {
126        LengthHint::exact(self.len_utf8())
127    }
128
129    #[inline]
130    #[cfg(feature = "alloc")]
131    fn write_to_string(&self) -> Cow<'_, str> {
132        let mut s = String::with_capacity(self.len_utf8());
133        s.push(*self);
134        Cow::Owned(s)
135    }
136}
137
138impl<T: Writeable + ?Sized> crate::Writeable for &T {
    #[inline]
    fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
        -> core::fmt::Result {
        (*self).write_to(sink)
    }
    #[inline]
    fn write_to_parts<S: crate::PartsWrite + ?Sized>(&self, sink: &mut S)
        -> core::fmt::Result {
        (*self).write_to_parts(sink)
    }
    #[inline]
    fn writeable_length_hint(&self) -> crate::LengthHint {
        (*self).writeable_length_hint()
    }
    #[inline]
    fn writeable_borrow(&self) -> Option<&str> { (*self).writeable_borrow() }
}crate::impl_writeable_delegate!(&T, |&self| *self, #[cfg(feature = "alloc")] fn write_to_string, where T: Writeable + ?Sized);
139
140#[cfg(feature = "alloc")]
141macro_rules! impl_write_smart_pointer {
142    ($ty:path, T: $extra_bound:path) => {
143        #[allow(unused_qualifications)]
144        impl<T: ?Sized + Writeable + $extra_bound> Writeable for $ty {
145            #[inline]
146            fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
147                core::borrow::Borrow::<T>::borrow(self).write_to(sink)
148            }
149            #[inline]
150            fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> fmt::Result {
151                core::borrow::Borrow::<T>::borrow(self).write_to_parts(sink)
152            }
153            #[inline]
154            fn writeable_length_hint(&self) -> LengthHint {
155                core::borrow::Borrow::<T>::borrow(self).writeable_length_hint()
156            }
157            #[inline]
158            fn writeable_borrow(&self) -> Option<&str> {
159                core::borrow::Borrow::<T>::borrow(self).writeable_borrow()
160            }
161            #[inline]
162            fn write_to_string(&self) -> Cow<'_, str> {
163                core::borrow::Borrow::<T>::borrow(self).write_to_string()
164            }
165        }
166    };
167    ($ty:path) => {
168        // Add a harmless duplicate Writeable bound
169        impl_write_smart_pointer!($ty, T: Writeable);
170    };
171}
172
173#[cfg(feature = "alloc")]
174impl_write_smart_pointer!(Cow<'_, T>, T: alloc::borrow::ToOwned);
175#[cfg(feature = "alloc")]
176impl_write_smart_pointer!(alloc::boxed::Box<T>);
177#[cfg(feature = "alloc")]
178impl_write_smart_pointer!(alloc::rc::Rc<T>);
179#[cfg(feature = "alloc")]
180impl_write_smart_pointer!(alloc::sync::Arc<T>);
181
182#[test]
183fn test_string_impls() {
184    fn check_writeable_slice<W: Writeable + core::fmt::Display>(writeables: &[W]) {
185        assert_writeable_eq!(&writeables[0], "");
186        assert_writeable_eq!(&writeables[1], "abc");
187        assert!(matches!(writeables[0].write_to_string(), Cow::Borrowed(_)));
188        assert!(matches!(writeables[1].write_to_string(), Cow::Borrowed(_)));
189    }
190
191    // test str impl
192    let arr: &[&str] = &["", "abc"];
193    check_writeable_slice(arr);
194
195    // test String impl
196    let arr: &[String] = &[String::new(), "abc".to_owned()];
197    check_writeable_slice(arr);
198
199    // test char impl
200    let chars = ['a', 'β', '你', '😀'];
201    for i in 0..chars.len() {
202        let s = String::from(chars[i]);
203        assert_writeable_eq!(&chars[i], s);
204        for j in 0..chars.len() {
205            assert_eq!(
206                cmp_str(&chars[j], &s),
207                chars[j].cmp(&chars[i]),
208                "{:?} vs {:?}",
209                chars[j],
210                chars[i]
211            );
212        }
213    }
214
215    // test Cow impl
216    let arr: &[Cow<str>] = &[Cow::Borrowed(""), Cow::Owned("abc".to_string())];
217    check_writeable_slice(arr);
218
219    // test Box impl
220    let arr: &[Box<str>] = &["".into(), "abc".into()];
221    check_writeable_slice(arr);
222
223    // test Rc impl
224    let arr: &[alloc::rc::Rc<str>] = &["".into(), "abc".into()];
225    check_writeable_slice(arr);
226
227    // test Arc impl
228    let arr: &[alloc::sync::Arc<str>] = &["".into(), "abc".into()];
229    check_writeable_slice(arr);
230
231    // test &T impl
232    let arr: &[&String] = &[&String::new(), &"abc".to_owned()];
233    check_writeable_slice(arr);
234}