Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const constructor functions #17

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ impl GUID {
/// assert_eq!(guid.data4(), [ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]);
/// assert_eq!(guid.to_string(), "87935CDE-7094-4C2B-A0F4-DD7D512DD261");
/// ```
pub fn build_from_components(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
let mut guid = GUID::default();
// first data
guid.data[..4].copy_from_slice(&d1.to_be_bytes());
// second data
guid.data[4..6].copy_from_slice(&d2.to_be_bytes());
// third data
guid.data[6..8].copy_from_slice(&d3.to_be_bytes());
// fourth data
guid.data[8..16].copy_from_slice(&d4[..8]);

guid
pub const fn build_from_components(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
let d1 = d1.to_be_bytes();
let d2 = d2.to_be_bytes();
let d3 = d3.to_be_bytes();
let data = [
d1[0], d1[1], d1[2], d1[3],
d2[0], d2[1],
d3[0], d3[1],
d4[0], d4[1], d4[2], d4[3], d4[4], d4[5], d4[6], d4[7],
];

GUID{ data }
}

/// Construct a `GUID` from 16 bytes.
Expand All @@ -103,7 +103,7 @@ impl GUID {
/// );
/// assert_eq!(guid.to_string(), "87935CDE-7094-4C2B-A0F4-DD7D512DD261");
/// ```
pub fn build_from_slice(data: &[u8; 16]) -> Self {
pub const fn build_from_slice(data: &[u8; 16]) -> Self {
GUID { data: *data }
}

Expand Down