Skip to content

Commit e87048c

Browse files
refactor: get rid of lazy_static (#1022)
1 parent 52225cc commit e87048c

File tree

52 files changed

+1569
-1551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1569
-1551
lines changed

Cargo.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ rstest = "0.25"
3939
# Note: we are trying to move away from using these crates.
4040
# They are being kept around for now for legacy compatibility,
4141
# but new usage should be avoided.
42-
lazy_static = "1.4" # Legacy crate; prefer std::sync::LazyLock or LazyCell
4342
num-derive = "0.4"
4443
num-traits = "0.2"
4544

crates/ironrdp-graphics/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ bitvec = "1.0"
2222
ironrdp-core = { path = "../ironrdp-core", version = "0.1" } # public
2323
ironrdp-pdu = { path = "../ironrdp-pdu", version = "0.6", features = ["std"] } # public
2424
byteorder = "1.5" # TODO: remove
25-
lazy_static.workspace = true # Legacy crate; prefer std::sync::LazyLock or LazyCell
2625
num-derive.workspace = true # TODO: remove
2726
num-traits.workspace = true # TODO: remove
2827
yuv = { version = "0.8", features = ["rdp"] }

crates/ironrdp-graphics/src/rectangle_processing.rs

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -400,88 +400,86 @@ fn bands_internals_equal(first_band: &[InclusiveRectangle], second_band: &[Inclu
400400

401401
#[cfg(test)]
402402
mod tests {
403-
use lazy_static::lazy_static;
403+
use std::sync::LazyLock;
404404

405405
use super::*;
406406

407-
lazy_static! {
408-
static ref REGION_FOR_RECTANGLES_INTERSECTION: Region = Region {
409-
extents: InclusiveRectangle {
407+
static REGION_FOR_RECTANGLES_INTERSECTION: LazyLock<Region> = LazyLock::new(|| Region {
408+
extents: InclusiveRectangle {
409+
left: 1,
410+
top: 1,
411+
right: 11,
412+
bottom: 9,
413+
},
414+
rectangles: vec![
415+
InclusiveRectangle {
410416
left: 1,
411417
top: 1,
418+
right: 5,
419+
bottom: 3,
420+
},
421+
InclusiveRectangle {
422+
left: 7,
423+
top: 1,
424+
right: 8,
425+
bottom: 3,
426+
},
427+
InclusiveRectangle {
428+
left: 9,
429+
top: 1,
412430
right: 11,
431+
bottom: 3,
432+
},
433+
InclusiveRectangle {
434+
left: 7,
435+
top: 3,
436+
right: 11,
437+
bottom: 4,
438+
},
439+
InclusiveRectangle {
440+
left: 3,
441+
top: 4,
442+
right: 6,
443+
bottom: 6,
444+
},
445+
InclusiveRectangle {
446+
left: 7,
447+
top: 4,
448+
right: 11,
449+
bottom: 6,
450+
},
451+
InclusiveRectangle {
452+
left: 1,
453+
top: 6,
454+
right: 3,
455+
bottom: 8,
456+
},
457+
InclusiveRectangle {
458+
left: 4,
459+
top: 6,
460+
right: 5,
461+
bottom: 8,
462+
},
463+
InclusiveRectangle {
464+
left: 6,
465+
top: 6,
466+
right: 10,
467+
bottom: 8,
468+
},
469+
InclusiveRectangle {
470+
left: 4,
471+
top: 8,
472+
right: 5,
413473
bottom: 9,
414474
},
415-
rectangles: vec![
416-
InclusiveRectangle {
417-
left: 1,
418-
top: 1,
419-
right: 5,
420-
bottom: 3,
421-
},
422-
InclusiveRectangle {
423-
left: 7,
424-
top: 1,
425-
right: 8,
426-
bottom: 3,
427-
},
428-
InclusiveRectangle {
429-
left: 9,
430-
top: 1,
431-
right: 11,
432-
bottom: 3,
433-
},
434-
InclusiveRectangle {
435-
left: 7,
436-
top: 3,
437-
right: 11,
438-
bottom: 4,
439-
},
440-
InclusiveRectangle {
441-
left: 3,
442-
top: 4,
443-
right: 6,
444-
bottom: 6,
445-
},
446-
InclusiveRectangle {
447-
left: 7,
448-
top: 4,
449-
right: 11,
450-
bottom: 6,
451-
},
452-
InclusiveRectangle {
453-
left: 1,
454-
top: 6,
455-
right: 3,
456-
bottom: 8,
457-
},
458-
InclusiveRectangle {
459-
left: 4,
460-
top: 6,
461-
right: 5,
462-
bottom: 8,
463-
},
464-
InclusiveRectangle {
465-
left: 6,
466-
top: 6,
467-
right: 10,
468-
bottom: 8,
469-
},
470-
InclusiveRectangle {
471-
left: 4,
472-
top: 8,
473-
right: 5,
474-
bottom: 9,
475-
},
476-
InclusiveRectangle {
477-
left: 6,
478-
top: 8,
479-
right: 10,
480-
bottom: 9,
481-
},
482-
],
483-
};
484-
}
475+
InclusiveRectangle {
476+
left: 6,
477+
top: 8,
478+
right: 10,
479+
bottom: 9,
480+
},
481+
],
482+
});
485483

486484
#[test]
487485
fn union_rectangle_sets_extents_and_single_rectangle_for_empty_region() {

crates/ironrdp-graphics/src/zgfx/control_messages.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bitflags! {
8484

8585
#[cfg(test)]
8686
mod test {
87-
use lazy_static::lazy_static;
87+
use std::sync::LazyLock;
8888

8989
use super::*;
9090

@@ -111,29 +111,30 @@ mod test {
111111
0x02, // the third segment: data
112112
];
113113

114-
lazy_static! {
115-
static ref SINGLE_SEGMENTED_DATA_PDU: SegmentedDataPdu<'static> = SegmentedDataPdu::Single(BulkEncodedData {
114+
static SINGLE_SEGMENTED_DATA_PDU: LazyLock<SegmentedDataPdu<'static>> = LazyLock::new(|| {
115+
SegmentedDataPdu::Single(BulkEncodedData {
116116
compression_flags: CompressionFlags::COMPRESSED,
117117
data: &SINGLE_SEGMENTED_DATA_PDU_BUFFER[2..],
118-
});
119-
static ref MULTIPART_SEGMENTED_DATA_PDU: SegmentedDataPdu<'static> = SegmentedDataPdu::Multipart {
118+
})
119+
});
120+
static MULTIPART_SEGMENTED_DATA_PDU: LazyLock<SegmentedDataPdu<'static>> =
121+
LazyLock::new(|| SegmentedDataPdu::Multipart {
120122
uncompressed_size: 0x2B,
121123
segments: vec![
122124
BulkEncodedData {
123125
compression_flags: CompressionFlags::empty(),
124-
data: &MULTIPART_SEGMENTED_DATA_PDU_BUFFER[12..12 + 16]
126+
data: &MULTIPART_SEGMENTED_DATA_PDU_BUFFER[12..12 + 16],
125127
},
126128
BulkEncodedData {
127129
compression_flags: CompressionFlags::empty(),
128-
data: &MULTIPART_SEGMENTED_DATA_PDU_BUFFER[33..33 + 13]
130+
data: &MULTIPART_SEGMENTED_DATA_PDU_BUFFER[33..33 + 13],
129131
},
130132
BulkEncodedData {
131133
compression_flags: CompressionFlags::COMPRESSED,
132-
data: &MULTIPART_SEGMENTED_DATA_PDU_BUFFER[51..]
134+
data: &MULTIPART_SEGMENTED_DATA_PDU_BUFFER[51..],
133135
},
134136
],
135-
};
136-
}
137+
});
137138

138139
#[test]
139140
fn from_buffer_correctly_parses_zgfx_single_segmented_data_pdu() {

crates/ironrdp-graphics/src/zgfx/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod circular_buffer;
44
mod control_messages;
55

66
use std::io::{self, Write as _};
7+
use std::sync::LazyLock;
78

89
use bitvec::bits;
910
use bitvec::field::BitField as _;
@@ -223,8 +224,8 @@ enum TokenType {
223224
},
224225
}
225226

226-
lazy_static::lazy_static! {
227-
static ref TOKEN_TABLE: [Token; 40] = [
227+
static TOKEN_TABLE: LazyLock<[Token; 40]> = LazyLock::new(|| {
228+
[
228229
Token {
229230
prefix: bits![static u8, Msb0; 0],
230231
ty: TokenType::NullLiteral,
@@ -427,8 +428,8 @@ lazy_static::lazy_static! {
427428
distance_base: 17_094_304,
428429
},
429430
},
430-
];
431-
}
431+
]
432+
});
432433

433434
#[derive(Debug)]
434435
pub enum ZgfxError {

crates/ironrdp-pdu/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pkcs1 = "0.7"
4444

4545
[dev-dependencies]
4646
expect-test.workspace = true
47-
lazy_static.workspace = true # TODO: remove in favor of https://doc.rust-lang.org/std/sync/struct.OnceLock.html
4847

4948
[lints]
5049
workspace = true

crates/ironrdp-pdu/src/basic_output/bitmap/tests.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use std::sync::LazyLock;
2+
13
use ironrdp_core::{decode, encode};
2-
use lazy_static::lazy_static;
34

45
use super::*;
56

@@ -31,31 +32,29 @@ const BITMAP_BUFFER: [u8; 114] = [
3132
0x55, 0xad, 0x10, 0x10, 0xa8, 0xd8, 0x60, 0x12,
3233
];
3334

34-
lazy_static! {
35-
static ref BITMAP: BitmapUpdateData<'static> = BitmapUpdateData {
36-
rectangles: {
37-
let vec = vec![BitmapData {
38-
rectangle: InclusiveRectangle {
39-
left: 1792,
40-
top: 1024,
41-
right: 1855,
42-
bottom: 1079,
43-
},
44-
width: 64,
45-
height: 56,
46-
bits_per_pixel: 16,
47-
compression_flags: Compression::BITMAP_COMPRESSION,
48-
compressed_data_header: Some(CompressedDataHeader {
49-
main_body_size: 80,
50-
scan_width: 28,
51-
uncompressed_size: 4,
52-
}),
53-
bitmap_data: &BITMAP_BUFFER[30..],
54-
}];
55-
vec
56-
}
57-
};
58-
}
35+
static BITMAP: LazyLock<BitmapUpdateData<'static>> = LazyLock::new(|| BitmapUpdateData {
36+
rectangles: {
37+
let vec = vec![BitmapData {
38+
rectangle: InclusiveRectangle {
39+
left: 1792,
40+
top: 1024,
41+
right: 1855,
42+
bottom: 1079,
43+
},
44+
width: 64,
45+
height: 56,
46+
bits_per_pixel: 16,
47+
compression_flags: Compression::BITMAP_COMPRESSION,
48+
compressed_data_header: Some(CompressedDataHeader {
49+
main_body_size: 80,
50+
scan_width: 28,
51+
uncompressed_size: 4,
52+
}),
53+
bitmap_data: &BITMAP_BUFFER[30..],
54+
}];
55+
vec
56+
},
57+
});
5958

6059
#[test]
6160
fn from_buffer_bitmap_data_parsses_correctly() {

crates/ironrdp-pdu/src/basic_output/fast_path/tests.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use std::sync::LazyLock;
2+
13
use ironrdp_core::{decode, encode};
2-
use lazy_static::lazy_static;
34

45
use super::*;
56

@@ -29,15 +30,13 @@ const FAST_PATH_HEADER_WITH_FORCED_LONG_LEN_PDU: FastPathHeader = FastPathHeader
2930
forced_long_length: true,
3031
};
3132

32-
lazy_static! {
33-
static ref FAST_PATH_UPDATE_PDU: FastPathUpdatePdu<'static> = FastPathUpdatePdu {
34-
fragmentation: Fragmentation::Single,
35-
update_code: UpdateCode::SurfaceCommands,
36-
compression_flags: None,
37-
compression_type: None,
38-
data: &FAST_PATH_UPDATE_PDU_BUFFER[3..],
39-
};
40-
}
33+
static FAST_PATH_UPDATE_PDU: LazyLock<FastPathUpdatePdu<'static>> = LazyLock::new(|| FastPathUpdatePdu {
34+
fragmentation: Fragmentation::Single,
35+
update_code: UpdateCode::SurfaceCommands,
36+
compression_flags: None,
37+
compression_type: None,
38+
data: &FAST_PATH_UPDATE_PDU_BUFFER[3..],
39+
});
4140

4241
#[test]
4342
fn from_buffer_correctly_parses_fast_path_header_with_short_length() {

0 commit comments

Comments
 (0)