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                while n != 0 {
19                    i -= 1;
20                    buf[i] = b'0' + (n % 10) as u8;
21                    n /= 10;
22                }
23                if i == MAX_LEN {
24                    debug_assert_eq!(*self, 0);
25                    i -= 1;
26                }
27                #[expect(clippy::indexing_slicing)] // buf is ASCII
28                let s = unsafe { core::str::from_utf8_unchecked(&buf[i..]) };
29                sink.write_str(s)
30            }
31
32            fn writeable_length_hint(&self) -> $crate::LengthHint {
33                LengthHint::exact(self.checked_ilog10().unwrap_or(0) as usize + 1)
34            }
35        }
36
37        impl $crate::Writeable for $i {
38            fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
39                if self.is_negative() {
40                    sink.write_str("-")?;
41                }
42                self.unsigned_abs().write_to(sink)
43            }
44
45            fn writeable_length_hint(&self) -> $crate::LengthHint {
46                $crate::LengthHint::exact(if self.is_negative() { 1 } else { 0 })
47                    + self.unsigned_abs().writeable_length_hint()
48            }
49        }
50
51        #[test]
52        fn $test() {
53            use $crate::assert_writeable_eq;
54            assert_writeable_eq!(&(0 as $u), "0");
55            assert_writeable_eq!(&(0 as $i), "0");
56            assert_writeable_eq!(&(-0 as $i), "0");
57            assert_writeable_eq!(&(1 as $u), "1");
58            assert_writeable_eq!(&(1 as $i), "1");
59            assert_writeable_eq!(&(-1 as $i), "-1");
60            assert_writeable_eq!(&(9 as $u), "9");
61            assert_writeable_eq!(&(9 as $i), "9");
62            assert_writeable_eq!(&(-9 as $i), "-9");
63            assert_writeable_eq!(&(10 as $u), "10");
64            assert_writeable_eq!(&(10 as $i), "10");
65            assert_writeable_eq!(&(-10 as $i), "-10");
66            assert_writeable_eq!(&(99 as $u), "99");
67            assert_writeable_eq!(&(99 as $i), "99");
68            assert_writeable_eq!(&(-99 as $i), "-99");
69            assert_writeable_eq!(&(100 as $u), "100");
70            assert_writeable_eq!(&(-100 as $i), "-100");
71            assert_writeable_eq!(&<$u>::MAX, <$u>::MAX.to_string());
72            assert_writeable_eq!(&<$i>::MAX, <$i>::MAX.to_string());
73            assert_writeable_eq!(&<$i>::MIN, <$i>::MIN.to_string());
74
75            $(
76
77                use rand::{rngs::SmallRng, Rng, SeedableRng};
78                let mut rng = SmallRng::seed_from_u64(4); // chosen by fair dice roll.
79                                                          // guaranteed to be random.
80                for _ in 0..1000 {
81                    let rand = rng.$random_call::<$u>();
82                    assert_writeable_eq!(rand, rand.to_string());
83                }
84            )?
85        }
86    };
87}
88
89impl 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)]
        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);
90impl 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)]
        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);
91impl 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)]
        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);
92impl 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)]
        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);
93impl 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)]
        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);
94impl 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)]
        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);
95
96impl Writeable for str {
97    #[inline]
98    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
99        sink.write_str(self)
100    }
101
102    #[inline]
103    fn writeable_length_hint(&self) -> LengthHint {
104        LengthHint::exact(self.len())
105    }
106
107    #[inline]
108    fn writeable_borrow(&self) -> Option<&str> {
109        Some(self)
110    }
111}
112
113#[cfg(feature = "alloc")]
114impl Writeable for String {
115    #[inline]
116    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
117        sink.write_str(self)
118    }
119
120    #[inline]
121    fn writeable_length_hint(&self) -> LengthHint {
122        LengthHint::exact(self.len())
123    }
124
125    #[inline]
126    fn writeable_borrow(&self) -> Option<&str> {
127        Some(self)
128    }
129}
130
131impl Writeable for char {
132    #[inline]
133    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
134        sink.write_char(*self)
135    }
136
137    #[inline]
138    fn writeable_length_hint(&self) -> LengthHint {
139        LengthHint::exact(self.len_utf8())
140    }
141
142    #[inline]
143    #[cfg(feature = "alloc")]
144    fn write_to_string(&self) -> Cow<'_, str> {
145        let mut s = String::with_capacity(self.len_utf8());
146        s.push(*self);
147        Cow::Owned(s)
148    }
149}
150
151impl<T: Writeable + ?Sized> Writeable for &T {
152    #[inline]
153    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
154        (*self).write_to(sink)
155    }
156
157    #[inline]
158    fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> fmt::Result {
159        (*self).write_to_parts(sink)
160    }
161
162    #[inline]
163    fn writeable_length_hint(&self) -> LengthHint {
164        (*self).writeable_length_hint()
165    }
166
167    #[inline]
168    fn writeable_borrow(&self) -> Option<&str> {
169        (*self).writeable_borrow()
170    }
171
172    #[inline]
173    #[cfg(feature = "alloc")]
174    fn write_to_string(&self) -> Cow<'_, str> {
175        (*self).write_to_string()
176    }
177}
178
179#[cfg(feature = "alloc")]
180macro_rules! impl_write_smart_pointer {
181    ($ty:path, T: $extra_bound:path) => {
182        impl<'a, T: ?Sized + Writeable + $extra_bound> Writeable for $ty {
183            #[inline]
184            fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
185                core::borrow::Borrow::<T>::borrow(self).write_to(sink)
186            }
187            #[inline]
188            fn write_to_parts<W: PartsWrite + ?Sized>(&self, sink: &mut W) -> fmt::Result {
189                core::borrow::Borrow::<T>::borrow(self).write_to_parts(sink)
190            }
191            #[inline]
192            fn writeable_length_hint(&self) -> LengthHint {
193                core::borrow::Borrow::<T>::borrow(self).writeable_length_hint()
194            }
195            #[inline]
196            fn writeable_borrow(&self) -> Option<&str> {
197                core::borrow::Borrow::<T>::borrow(self).writeable_borrow()
198            }
199            #[inline]
200            fn write_to_string(&self) -> Cow<'_, str> {
201                core::borrow::Borrow::<T>::borrow(self).write_to_string()
202            }
203        }
204    };
205    ($ty:path) => {
206        // Add a harmless duplicate Writeable bound
207        impl_write_smart_pointer!($ty, T: Writeable);
208    };
209}
210
211#[cfg(feature = "alloc")]
212impl_write_smart_pointer!(Cow<'a, T>, T: alloc::borrow::ToOwned);
213#[cfg(feature = "alloc")]
214impl_write_smart_pointer!(alloc::boxed::Box<T>);
215#[cfg(feature = "alloc")]
216impl_write_smart_pointer!(alloc::rc::Rc<T>);
217#[cfg(feature = "alloc")]
218impl_write_smart_pointer!(alloc::sync::Arc<T>);
219
220#[test]
221fn test_string_impls() {
222    fn check_writeable_slice<W: Writeable + core::fmt::Display>(writeables: &[W]) {
223        assert_writeable_eq!(&writeables[0], "");
224        assert_writeable_eq!(&writeables[1], "abc");
225        assert!(matches!(writeables[0].write_to_string(), Cow::Borrowed(_)));
226        assert!(matches!(writeables[1].write_to_string(), Cow::Borrowed(_)));
227    }
228
229    // test str impl
230    let arr: &[&str] = &["", "abc"];
231    check_writeable_slice(arr);
232
233    // test String impl
234    let arr: &[String] = &[String::new(), "abc".to_owned()];
235    check_writeable_slice(arr);
236
237    // test char impl
238    let chars = ['a', 'β', '你', '😀'];
239    for i in 0..chars.len() {
240        let s = String::from(chars[i]);
241        assert_writeable_eq!(&chars[i], s);
242        for j in 0..chars.len() {
243            assert_eq!(
244                crate::cmp_str(&chars[j], &s),
245                chars[j].cmp(&chars[i]),
246                "{:?} vs {:?}",
247                chars[j],
248                chars[i]
249            );
250        }
251    }
252
253    // test Cow impl
254    let arr: &[Cow<str>] = &[Cow::Borrowed(""), Cow::Owned("abc".to_string())];
255    check_writeable_slice(arr);
256
257    // test Box impl
258    let arr: &[Box<str>] = &["".into(), "abc".into()];
259    check_writeable_slice(arr);
260
261    // test Rc impl
262    let arr: &[alloc::rc::Rc<str>] = &["".into(), "abc".into()];
263    check_writeable_slice(arr);
264
265    // test Arc impl
266    let arr: &[alloc::sync::Arc<str>] = &["".into(), "abc".into()];
267    check_writeable_slice(arr);
268
269    // test &T impl
270    let arr: &[&String] = &[&String::new(), &"abc".to_owned()];
271    check_writeable_slice(arr);
272}