@@ -11,15 +11,23 @@ use {
11
11
12
12
/// A measure of text length. Also, equivalently, an index into text.
13
13
///
14
- /// This is a utf8- bytes- offset stored as `u32`, but
14
+ /// This is a UTF-8 bytes offset stored as `u32`, but
15
15
/// most clients should treat it as an opaque measure.
16
16
///
17
+ /// For cases that need to escape `TextSize` and return to working directly
18
+ /// with primitive integers, `TextSize` can be converted losslessly to/from
19
+ /// `u32` via [`From`] conversions as well as losslessly be converted [`Into`]
20
+ /// `usize`. The `usize -> TextSize` direction can be done via [`TryFrom`].
21
+ ///
22
+ /// These escape hatches are primarily required for unit testing and when
23
+ /// converting from UTF-8 size to another coordinate space, such as UTF-16.
24
+ ///
17
25
/// # Translation from `text_unit`
18
26
///
19
27
/// - `TextUnit::of_char(c)` ⟹ `TextSize::of(c)`
20
- /// - `TextUnit::of_str(s)` ⟹ `TextSize:of(s)`
28
+ /// - `TextUnit::of_str(s)` ⟹ `TextSize:: of(s)`
21
29
/// - `TextUnit::from_usize(size)` ⟹ `TextSize::try_from(size).unwrap_or_else(|| panic!(_))`
22
- /// - `unit.to_usize()` ⟹ `usize::try_from (size).unwrap_or_else(|| panic!(_) )`
30
+ /// - `unit.to_usize()` ⟹ `usize::from (size)`
23
31
#[ derive( Clone , Copy , Default , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
24
32
pub struct TextSize {
25
33
pub ( crate ) raw : u32 ,
@@ -38,6 +46,7 @@ impl fmt::Debug for TextSize {
38
46
39
47
impl TextSize {
40
48
/// The text size of some text-like object.
49
+ #[ inline]
41
50
pub fn of ( text : impl TextSized ) -> TextSize {
42
51
text. text_size ( )
43
52
}
@@ -46,6 +55,7 @@ impl TextSize {
46
55
///
47
56
/// This is equivalent to `TextSize::default()` or [`TextSize::MIN`],
48
57
/// but is more explicit on intent.
58
+ #[ inline]
49
59
pub const fn zero ( ) -> TextSize {
50
60
TextSize ( 0 )
51
61
}
@@ -59,57 +69,60 @@ impl TextSize {
59
69
/// The largest representable text size. (`u32::MAX`)
60
70
pub const MAX : TextSize = TextSize ( u32:: MAX ) ;
61
71
62
- #[ allow( missing_docs) ]
72
+ /// Checked addition. Returns `None` if overflow occurred.
73
+ #[ inline]
63
74
pub fn checked_add ( self , rhs : TextSize ) -> Option < TextSize > {
64
75
self . raw . checked_add ( rhs. raw ) . map ( TextSize )
65
76
}
66
77
67
- #[ allow( missing_docs) ]
78
+ /// Checked subtraction. Returns `None` if overflow occurred.
79
+ #[ inline]
68
80
pub fn checked_sub ( self , rhs : TextSize ) -> Option < TextSize > {
69
81
self . raw . checked_sub ( rhs. raw ) . map ( TextSize )
70
82
}
71
83
}
72
84
73
85
impl From < u32 > for TextSize {
86
+ #[ inline]
74
87
fn from ( raw : u32 ) -> Self {
75
- TextSize { raw }
88
+ TextSize ( raw)
76
89
}
77
90
}
78
91
79
92
impl From < TextSize > for u32 {
93
+ #[ inline]
80
94
fn from ( value : TextSize ) -> Self {
81
95
value. raw
82
96
}
83
97
}
84
98
85
99
impl TryFrom < usize > for TextSize {
86
100
type Error = TryFromIntError ;
101
+ #[ inline]
87
102
fn try_from ( value : usize ) -> Result < Self , TryFromIntError > {
88
103
Ok ( u32:: try_from ( value) ?. into ( ) )
89
104
}
90
105
}
91
106
92
107
impl From < TextSize > for usize {
108
+ #[ inline]
93
109
fn from ( value : TextSize ) -> Self {
94
- assert_lossless_conversion ( ) ;
95
- return value. raw as usize ;
96
-
97
- const fn assert_lossless_conversion ( ) {
98
- [ ( ) ] [ ( std:: mem:: size_of :: < usize > ( ) < std:: mem:: size_of :: < u32 > ( ) ) as usize ]
99
- }
110
+ value. raw as usize
100
111
}
101
112
}
102
113
103
114
macro_rules! ops {
104
115
( impl $Op: ident for TextSize by fn $f: ident = $op: tt) => {
105
116
impl $Op<TextSize > for TextSize {
106
117
type Output = TextSize ;
118
+ #[ inline]
107
119
fn $f( self , other: TextSize ) -> TextSize {
108
120
TextSize ( self . raw $op other. raw)
109
121
}
110
122
}
111
123
impl $Op<& TextSize > for TextSize {
112
124
type Output = TextSize ;
125
+ #[ inline]
113
126
fn $f( self , other: & TextSize ) -> TextSize {
114
127
self $op * other
115
128
}
@@ -119,6 +132,7 @@ macro_rules! ops {
119
132
TextSize : $Op<T , Output =TextSize >,
120
133
{
121
134
type Output = TextSize ;
135
+ #[ inline]
122
136
fn $f( self , other: T ) -> TextSize {
123
137
* self $op other
124
138
}
@@ -133,6 +147,7 @@ impl<A> AddAssign<A> for TextSize
133
147
where
134
148
TextSize : Add < A , Output = TextSize > ,
135
149
{
150
+ #[ inline]
136
151
fn add_assign ( & mut self , rhs : A ) {
137
152
* self = * self + rhs
138
153
}
@@ -142,6 +157,7 @@ impl<S> SubAssign<S> for TextSize
142
157
where
143
158
TextSize : Sub < S , Output = TextSize > ,
144
159
{
160
+ #[ inline]
145
161
fn sub_assign ( & mut self , rhs : S ) {
146
162
* self = * self - rhs
147
163
}
@@ -151,6 +167,7 @@ impl<A> iter::Sum<A> for TextSize
151
167
where
152
168
TextSize : Add < A , Output = TextSize > ,
153
169
{
170
+ #[ inline]
154
171
fn sum < I : Iterator < Item = A > > ( iter : I ) -> TextSize {
155
172
iter. fold ( TextSize :: zero ( ) , Add :: add)
156
173
}
0 commit comments