Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use {
    crate::TextSized,
    std::{
        convert::{TryFrom, TryInto},
        fmt, iter,
        num::TryFromIntError,
        ops::{Add, AddAssign, Sub, SubAssign},
        u32,
    },
};

/// A measure of text length. Also, equivalently, an index into text.
///
/// This is a utf8-bytes-offset stored as `u32`, but
/// most clients should treat it as an opaque measure.
///
/// # Translation from `text_unit`
///
/// - `TextUnit::of_char(c)` ⟹ `TextSize::of(c)`
/// - `TextUnit::of_str(s)` ⟹ `TextSize:of(s)`
/// - `TextUnit::from_usize(size)` ⟹ `TextSize::new(size)`
/// - `unit.to_usize()` ⟹ `size.ix()`
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TextSize {
    pub(crate) raw: u32,
}

#[allow(non_snake_case)]
pub(crate) const fn TextSize(raw: u32) -> TextSize {
    TextSize { raw }
}

impl fmt::Debug for TextSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for TextSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.raw, f)
    }
}

impl TextSize {
    /// The text size of some text-like object.
    pub fn of(text: &impl TextSized) -> TextSize {
        text.text_size()
    }

    /// A text size for some `usize`.
    ///
    /// # Panics
    ///
    /// Panics if the size is greater than `u32::MAX` and debug assertions are
    /// enabled. If debug assertions are not enabled, wraps into `u32` space.
    pub fn new(size: usize) -> TextSize {
        if let Ok(size) = size.try_into() {
            size
        } else if cfg!(debug_assertions) {
            panic!("overflow when converting to TextSize");
        } else {
            TextSize(size as u32)
        }
    }

    /// Convert this text size into the standard indexing type.
    ///
    /// # Panics
    ///
    /// Panics if the size is greater than `usize::MAX`. This can only
    /// occur on targets where `size_of::<usize>() < size_of::<u32>()`.
    pub fn ix(self) -> usize {
        if let Ok(ix) = self.try_into() {
            ix
        } else {
            panic!("overflow when converting TextSize to usize index")
        }
    }
}

/// Methods to act like a primitive integer type, where reasonably applicable.
//  Last updated for parity with Rust 1.42.0.
impl TextSize {
    /// The smallest representable text size. (`u32::MIN`)
    pub const MIN: TextSize = TextSize(u32::MIN);
    /// The largest representable text size. (`u32::MAX`)
    pub const MAX: TextSize = TextSize(u32::MAX);

    #[allow(missing_docs)]
    pub fn checked_add(self, rhs: impl TryInto<TextSize>) -> Option<TextSize> {
        let rhs = rhs.try_into().ok()?;
        self.raw.checked_add(rhs.raw).map(TextSize)
    }

    #[allow(missing_docs)]
    pub fn checked_sub(self, rhs: impl TryInto<TextSize>) -> Option<TextSize> {
        let rhs = rhs.try_into().ok()?;
        self.raw.checked_sub(rhs.raw).map(TextSize)
    }
}

macro_rules! conversions {
    (From<TextSize> for $gte:ident) => {
        impl From<TextSize> for $gte {
            fn from(value: TextSize) -> $gte {
                value.raw.into()
            }
        }
    };
    (From<$lte:ident> for TextSize) => {
        impl From<$lte> for TextSize {
            fn from(value: $lte) -> TextSize {
                TextSize(value.into())
            }
        }
    };
    (TryFrom<TextSize> for $lt:ident) => {
        impl TryFrom<TextSize> for $lt {
            type Error = TryFromIntError;
            fn try_from(value: TextSize) -> Result<$lt, Self::Error> {
                value.raw.try_into()
            }
        }
    };
    (TryFrom<$gt:ident> for TextSize) => {
        impl TryFrom<$gt> for TextSize {
            type Error = <$gt as TryInto<u32>>::Error;
            fn try_from(value: $gt) -> Result<TextSize, Self::Error> {
                value.try_into().map(TextSize)
            }
        }
    };
    {
        lt u32  [$($lt:ident)*]
        eq u32  [$($eq:ident)*]
        gt u32  [$($gt:ident)*]
        varries [$($var:ident)*]
    } => {
        $(
            // Not `From` yet because of integer type fallback. We want e.g.
            // `TextSize::from(0)` and `size + 1` to work, and more `From`
            // impls means that this will try (and fail) to use i32 rather
            // than one of the unsigned integer types that actually work.
            conversions!(TryFrom<$lt> for TextSize);
            conversions!(TryFrom<TextSize> for $lt);
        )*

        $(
            conversions!(From<$eq> for TextSize);
            conversions!(From<TextSize> for $eq);
        )*

        $(
            conversions!(TryFrom<$gt> for TextSize);
            conversions!(From<TextSize> for $gt);
        )*

        $(
            conversions!(TryFrom<$var> for TextSize);
            conversions!(TryFrom<TextSize> for $var);
        )*
    };
}

conversions! {
    lt u32  [u8 u16]
    eq u32  [u32]
    gt u32  [u64]
    varries [usize i32] // i32 so that `checked_add($lit)` (`try_from($lit)`) can work
    // this will unfortunately have to hang around even if integer literal type fallback improves
}

impl Into<TextSize> for &'_ TextSize {
    fn into(self) -> TextSize {
        *self
    }
}

impl Into<TextSize> for &'_ mut TextSize {
    fn into(self) -> TextSize {
        *self
    }
}

macro_rules! op {
    (impl $Op:ident for TextSize by fn $f:ident = $op:tt) => {
        impl<IntoSize: Into<TextSize>> $Op<IntoSize> for TextSize {
            type Output = TextSize;
            fn $f(self, rhs: IntoSize) -> TextSize {
                TextSize(self.raw $op rhs.into().raw)
            }
        }
        impl<IntoSize> $Op<IntoSize> for &'_ TextSize
        where
            TextSize: $Op<IntoSize, Output = TextSize>,
        {
            type Output = TextSize;
            fn $f(self, rhs: IntoSize) -> TextSize {
                *self $op rhs
            }
        }
        impl<IntoSize> $Op<IntoSize> for &'_ mut TextSize
        where
            TextSize: $Op<IntoSize, Output = TextSize>,
        {
            type Output = TextSize;
            fn $f(self, rhs: IntoSize) -> TextSize {
                *self $op rhs
            }
        }
    };
}

op!(impl Add for TextSize by fn add = +);
op!(impl Sub for TextSize by fn sub = -);

impl<A> AddAssign<A> for TextSize
where
    TextSize: Add<A, Output = TextSize>,
{
    fn add_assign(&mut self, rhs: A) {
        *self = *self + rhs
    }
}

impl<S> SubAssign<S> for TextSize
where
    TextSize: Sub<S, Output = TextSize>,
{
    fn sub_assign(&mut self, rhs: S) {
        *self = *self - rhs
    }
}

impl iter::Sum for TextSize {
    fn sum<I: Iterator<Item = TextSize>>(iter: I) -> TextSize {
        iter.fold(TextSize::default(), Add::add)
    }
}

impl<'a> iter::Sum<&'a Self> for TextSize {
    fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
        iter.fold(TextSize::default(), Add::add)
    }
}