1
1
use std:: {
2
2
cell:: { RefCell , RefMut } ,
3
- sync:: Arc ,
4
3
time:: SystemTime ,
5
4
} ;
6
5
@@ -36,25 +35,25 @@ struct CacheValues {
36
35
is_leap_second : bool ,
37
36
full_second_str : RefCell < Option < String > > ,
38
37
year : RefCell < Option < i32 > > ,
39
- year_str : RefCell < Option < Arc < String > > > ,
40
- year_short_str : RefCell < Option < Arc < String > > > ,
38
+ year_str : RefCell < Option < String > > ,
39
+ year_short_str : RefCell < Option < String > > ,
41
40
month : RefCell < Option < u32 > > ,
42
- month_str : RefCell < Option < Arc < String > > > ,
41
+ month_str : RefCell < Option < String > > ,
43
42
month_name : RefCell < Option < MultiName < & ' static str > > > ,
44
43
weekday_name : RefCell < Option < MultiName < & ' static str > > > ,
45
44
day : RefCell < Option < u32 > > ,
46
- day_str : RefCell < Option < Arc < String > > > ,
45
+ day_str : RefCell < Option < String > > ,
47
46
hour : RefCell < Option < u32 > > ,
48
- hour_str : RefCell < Option < Arc < String > > > ,
47
+ hour_str : RefCell < Option < String > > ,
49
48
hour12 : RefCell < Option < ( bool , u32 ) > > ,
50
- hour12_str : RefCell < Option < Arc < String > > > ,
49
+ hour12_str : RefCell < Option < String > > ,
51
50
am_pm_str : RefCell < Option < & ' static str > > ,
52
51
minute : RefCell < Option < u32 > > ,
53
- minute_str : RefCell < Option < Arc < String > > > ,
52
+ minute_str : RefCell < Option < String > > ,
54
53
second : RefCell < Option < u32 > > ,
55
- second_str : RefCell < Option < Arc < String > > > ,
56
- tz_offset_str : RefCell < Option < Arc < String > > > ,
57
- unix_timestamp_str : RefCell < Option < Arc < String > > > ,
54
+ second_str : RefCell < Option < String > > ,
55
+ tz_offset_str : RefCell < Option < String > > ,
56
+ unix_timestamp_str : RefCell < Option < String > > ,
58
57
}
59
58
60
59
#[ derive( Clone , Copy , Eq , PartialEq ) ]
@@ -119,12 +118,12 @@ macro_rules! impl_cache_fields_getter {
119
118
macro_rules! impl_cache_fields_str_getter {
120
119
( $( $field: ident => $str_field: ident : $fmt: literal) ,* $( , ) ? ) => {
121
120
#[ must_use]
122
- $( pub ( crate ) fn $str_field( & self ) -> Arc < String > {
123
- self . cached
124
- . $str_field
125
- . borrow_mut ( )
126
- . get_or_insert_with ( || Arc :: new ( format! ( $fmt , self . cached . local_time . $field ( ) ) ) )
127
- . clone ( )
121
+ $( pub ( crate ) fn $str_field( & self ) -> RefMut < str > {
122
+ let mut value = self . cached. $str_field . borrow_mut ( ) ;
123
+ if value . is_none ( ) {
124
+ * value = Some ( format! ( $fmt , self . cached . local_time . $field ( ) ) ) ;
125
+ }
126
+ RefMut :: map ( value , |value| value . as_deref_mut ( ) . unwrap ( ) )
128
127
} ) *
129
128
} ;
130
129
}
@@ -262,12 +261,12 @@ impl<'a> TimeDate<'a> {
262
261
}
263
262
264
263
#[ must_use]
265
- pub ( crate ) fn hour12_str ( & self ) -> Arc < String > {
266
- self . cached
267
- . hour12_str
268
- . borrow_mut ( )
269
- . get_or_insert_with ( || Arc :: new ( format ! ( "{:02}" , self . hour12 ( ) . 1 ) ) )
270
- . clone ( )
264
+ pub ( crate ) fn hour12_str ( & self ) -> RefMut < str > {
265
+ let mut value = self . cached . hour12_str . borrow_mut ( ) ;
266
+ if value . is_none ( ) {
267
+ * value = Some ( format ! ( "{:02}" , self . hour12 ( ) . 1 ) ) ;
268
+ }
269
+ RefMut :: map ( value , |value| value . as_deref_mut ( ) . unwrap ( ) )
271
270
}
272
271
273
272
#[ must_use]
@@ -282,32 +281,33 @@ impl<'a> TimeDate<'a> {
282
281
}
283
282
284
283
#[ must_use]
285
- pub ( crate ) fn year_short_str ( & self ) -> Arc < String > {
286
- self . cached
287
- . year_short_str
288
- . borrow_mut ( )
289
- . get_or_insert_with ( || Arc :: new ( format ! ( "{:02}" , self . year ( ) % 100 ) ) )
290
- . clone ( )
284
+ pub ( crate ) fn year_short_str ( & self ) -> RefMut < str > {
285
+ let mut value = self . cached . year_short_str . borrow_mut ( ) ;
286
+ if value . is_none ( ) {
287
+ * value = Some ( format ! ( "{:02}" , self . year ( ) % 100 ) ) ;
288
+ }
289
+ RefMut :: map ( value , |value| value . as_deref_mut ( ) . unwrap ( ) )
291
290
}
292
291
293
292
#[ must_use]
294
- pub ( crate ) fn tz_offset_str ( & self ) -> Arc < String > {
295
- self . cached
296
- . tz_offset_str
297
- . borrow_mut ( )
298
- . get_or_insert_with ( || {
293
+ pub ( crate ) fn tz_offset_str ( & self ) -> RefMut < str > {
294
+ let mut value = self . cached . tz_offset_str . borrow_mut ( ) ;
295
+ if value. is_none ( ) {
296
+ * value = {
299
297
let offset_secs = self . cached . local_time . offset ( ) . local_minus_utc ( ) ;
300
298
let offset_secs_abs = offset_secs. abs ( ) ;
301
299
302
300
let sign_str = if offset_secs >= 0 { "+" } else { "-" } ;
303
301
let offset_hours = offset_secs_abs / 3600 ;
304
302
let offset_minutes = offset_secs_abs % 3600 / 60 ;
305
- Arc :: new ( format ! (
303
+
304
+ Some ( format ! (
306
305
"{}{:02}:{:02}" ,
307
306
sign_str, offset_hours, offset_minutes
308
307
) )
309
- } )
310
- . clone ( )
308
+ } ;
309
+ }
310
+ RefMut :: map ( value, |value| value. as_deref_mut ( ) . unwrap ( ) )
311
311
}
312
312
}
313
313
0 commit comments