From 5894e5167bdf3ad0be985b159d8ff17fd8b94a35 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Fri, 28 Apr 2023 23:16:59 -0400 Subject: [PATCH] Use the uguid crate to replace the `Guid` struct and `guid!` macro This should be mostly transparent for the end user except that the `from_values` method has been removed. Usually the `guid!` macro is a more convenient choice anyway, but `new` or `from_bytes` can also be used if needed. This allows us to delete a bit of code and some tests, as well as slightly reducing our dependency on proc macros as the `uguid::guid!` macro is a declarative macro. --- CHANGELOG.md | 5 + Cargo.lock | 56 +------ uefi-macros/Cargo.toml | 1 - uefi-macros/src/lib.rs | 102 +----------- uefi-macros/tests/compilation.rs | 5 - uefi-macros/tests/ui/entry_bad_abi.rs | 10 -- uefi-macros/tests/ui/entry_bad_abi.stderr | 5 - uefi-macros/tests/ui/entry_bad_arg.rs | 10 -- uefi-macros/tests/ui/entry_bad_arg.stderr | 8 - uefi-macros/tests/ui/entry_bad_async.rs | 10 -- uefi-macros/tests/ui/entry_bad_async.stderr | 5 - uefi-macros/tests/ui/entry_bad_attr_arg.rs | 10 -- .../tests/ui/entry_bad_attr_arg.stderr | 5 - uefi-macros/tests/ui/entry_bad_const.rs | 10 -- uefi-macros/tests/ui/entry_bad_const.stderr | 5 - uefi-macros/tests/ui/entry_bad_generic.rs | 10 -- uefi-macros/tests/ui/entry_bad_generic.stderr | 5 - uefi-macros/tests/ui/entry_bad_return_type.rs | 10 -- .../tests/ui/entry_bad_return_type.stderr | 8 - .../tests/ui/entry_unnamed_image_arg.rs | 10 -- .../tests/ui/entry_unnamed_image_arg.stderr | 5 - .../tests/ui/entry_unnamed_table_arg.rs | 10 -- .../tests/ui/entry_unnamed_table_arg.stderr | 5 - uefi-macros/tests/ui/guid_bad_hex_group2.rs | 7 - .../tests/ui/guid_bad_hex_group2.stderr | 5 - uefi-macros/tests/ui/guid_bad_hex_group5.rs | 7 - .../tests/ui/guid_bad_hex_group5.stderr | 5 - uefi-macros/tests/ui/guid_bad_length.rs | 7 - uefi-macros/tests/ui/guid_bad_length.stderr | 5 - uefi/Cargo.toml | 1 + uefi/src/data_types/guid.rs | 154 +----------------- uefi/src/lib.rs | 3 +- 32 files changed, 19 insertions(+), 485 deletions(-) delete mode 100644 uefi-macros/tests/compilation.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_abi.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_abi.stderr delete mode 100644 uefi-macros/tests/ui/entry_bad_arg.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_arg.stderr delete mode 100644 uefi-macros/tests/ui/entry_bad_async.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_async.stderr delete mode 100644 uefi-macros/tests/ui/entry_bad_attr_arg.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_attr_arg.stderr delete mode 100644 uefi-macros/tests/ui/entry_bad_const.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_const.stderr delete mode 100644 uefi-macros/tests/ui/entry_bad_generic.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_generic.stderr delete mode 100644 uefi-macros/tests/ui/entry_bad_return_type.rs delete mode 100644 uefi-macros/tests/ui/entry_bad_return_type.stderr delete mode 100644 uefi-macros/tests/ui/entry_unnamed_image_arg.rs delete mode 100644 uefi-macros/tests/ui/entry_unnamed_image_arg.stderr delete mode 100644 uefi-macros/tests/ui/entry_unnamed_table_arg.rs delete mode 100644 uefi-macros/tests/ui/entry_unnamed_table_arg.stderr delete mode 100644 uefi-macros/tests/ui/guid_bad_hex_group2.rs delete mode 100644 uefi-macros/tests/ui/guid_bad_hex_group2.stderr delete mode 100644 uefi-macros/tests/ui/guid_bad_hex_group5.rs delete mode 100644 uefi-macros/tests/ui/guid_bad_hex_group5.stderr delete mode 100644 uefi-macros/tests/ui/guid_bad_length.rs delete mode 100644 uefi-macros/tests/ui/guid_bad_length.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index a7139be87..46a743e89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,11 @@ `Status`. `impl From for Result` has also been removed. A new `StatusExt` trait has been added that provides conversion methods to replace the ones that have been removed. `StatusExt` has been added to the prelude. +- The `Guid` struct and `guid!` macro implementations have been replaced with + re-exports from the [`uguid`](https://docs.rs/uguid) crate. The `from_values` + method has been removed; usually the `guid!` macro is a more convenient + choice, but `new` or `from_bytes` can also be used if needed. There are also a + number of new `Guid` methods. ## uefi-macros - [Unreleased] diff --git a/Cargo.lock b/Cargo.lock index a7278b811..a0c1c36b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -185,12 +185,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" -[[package]] -name = "glob" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" - [[package]] name = "heck" version = "0.4.0" @@ -504,15 +498,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "termcolor" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" -dependencies = [ - "winapi-util", -] - [[package]] name = "thiserror" version = "1.0.37" @@ -533,30 +518,6 @@ dependencies = [ "syn 1.0.103", ] -[[package]] -name = "toml" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" -dependencies = [ - "serde", -] - -[[package]] -name = "trybuild" -version = "1.0.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea496675d71016e9bc76aa42d87f16aefd95447cc5818e671e12b2d7e269075d" -dependencies = [ - "glob", - "once_cell", - "serde", - "serde_derive", - "serde_json", - "termcolor", - "toml", -] - [[package]] name = "ucs2" version = "0.3.2" @@ -577,6 +538,7 @@ dependencies = [ "ucs2", "uefi-macros", "uefi-raw", + "uguid", ] [[package]] @@ -586,7 +548,6 @@ dependencies = [ "proc-macro2", "quote", "syn 2.0.8", - "trybuild", "uefi", ] @@ -627,6 +588,12 @@ dependencies = [ "uefi-services", ] +[[package]] +name = "uguid" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "594cc87e268a7b43d625d46c63cf1605d0e61bf66e4b1cd58c058ec0191e1f81" + [[package]] name = "unicode-ident" version = "1.0.5" @@ -649,15 +616,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/uefi-macros/Cargo.toml b/uefi-macros/Cargo.toml index 59550adcc..6e8ba2d82 100644 --- a/uefi-macros/Cargo.toml +++ b/uefi-macros/Cargo.toml @@ -20,5 +20,4 @@ quote = "1.0.9" syn = { version = "2.0.4", features = ["full"] } [dev-dependencies] -trybuild = "1.0.61" uefi = { version = "0.20.0", default-features = false } diff --git a/uefi-macros/src/lib.rs b/uefi-macros/src/lib.rs index a8acb51f5..c63429612 100644 --- a/uefi-macros/src/lib.rs +++ b/uefi-macros/src/lib.rs @@ -4,8 +4,8 @@ extern crate proc_macro; use proc_macro::TokenStream; -use proc_macro2::{TokenStream as TokenStream2, TokenTree}; -use quote::{quote, quote_spanned, ToTokens, TokenStreamExt}; +use proc_macro2::TokenStream as TokenStream2; +use quote::{quote, quote_spanned, TokenStreamExt}; use syn::spanned::Spanned; use syn::{ parse_macro_input, parse_quote, Error, Expr, ExprLit, ExprPath, FnArg, Ident, ItemFn, @@ -64,19 +64,7 @@ pub fn unsafe_protocol(args: TokenStream, input: TokenStream) -> TokenStream { Expr::Lit(ExprLit { lit: Lit::Str(lit), .. }) => { - // Parse as a GUID string. - let (time_low, time_mid, time_high_and_version, clock_seq_and_variant, node) = - match parse_guid(lit) { - Ok(data) => data, - Err(tokens) => return tokens.into(), - }; - quote!(::uefi::Guid::from_values( - #time_low, - #time_mid, - #time_high_and_version, - #clock_seq_and_variant, - #node, - )) + quote!(::uefi::guid!(#lit)) } Expr::Path(ExprPath { path, .. }) => quote!(#path), _ => { @@ -111,90 +99,6 @@ pub fn unsafe_protocol(args: TokenStream, input: TokenStream) -> TokenStream { .into() } -/// Create a `Guid` at compile time. -/// -/// # Example -/// -/// ``` -/// use uefi::{guid, Guid}; -/// const EXAMPLE_GUID: Guid = guid!("12345678-9abc-def0-1234-56789abcdef0"); -/// ``` -#[proc_macro] -pub fn guid(args: TokenStream) -> TokenStream { - let (time_low, time_mid, time_high_and_version, clock_seq_and_variant, node) = - match parse_guid(parse_macro_input!(args as LitStr)) { - Ok(data) => data, - Err(tokens) => return tokens.into(), - }; - - quote!({ - const g: ::uefi::Guid = ::uefi::Guid::from_values( - #time_low, - #time_mid, - #time_high_and_version, - #clock_seq_and_variant, - #node, - ); - g - }) - .into() -} - -fn parse_guid(guid_lit: LitStr) -> Result<(u32, u16, u16, u16, u64), TokenStream2> { - let guid_str = guid_lit.value(); - - // We expect a canonical GUID string, such as "12345678-9abc-def0-fedc-ba9876543210" - if guid_str.len() != 36 { - return Err(err!( - guid_lit, - "\"{}\" is not a canonical GUID string (expected 36 bytes, found {})", - guid_str, - guid_str.len() - )); - } - let mut offset = 1; // 1 is for the starting quote - let mut guid_hex_iter = guid_str.split('-'); - let mut next_guid_int = |len: usize| -> Result { - let guid_hex_component = guid_hex_iter.next().unwrap(); - - // convert syn::LitStr to proc_macro2::Literal.. - let lit = match guid_lit.to_token_stream().into_iter().next().unwrap() { - TokenTree::Literal(lit) => lit, - _ => unreachable!(), - }; - // ..so that we can call subspan and nightly users (us) will get the fancy span - let span = lit - .subspan(offset..offset + guid_hex_component.len()) - .unwrap_or_else(|| lit.span()); - - if guid_hex_component.len() != len * 2 { - return Err(err!( - span, - "GUID component \"{}\" is not a {}-bit hexadecimal string", - guid_hex_component, - len * 8 - )); - } - offset += guid_hex_component.len() + 1; // + 1 for the dash - u64::from_str_radix(guid_hex_component, 16).map_err(|_| { - err!( - span, - "GUID component \"{}\" is not a hexadecimal number", - guid_hex_component - ) - }) - }; - - // The GUID string is composed of a 32-bit integer, three 16-bit ones, and a 48-bit one - Ok(( - next_guid_int(4)? as u32, - next_guid_int(2)? as u16, - next_guid_int(2)? as u16, - next_guid_int(2)? as u16, - next_guid_int(6)?, - )) -} - /// Get the name of a function's argument at `arg_index`. fn get_function_arg_name(f: &ItemFn, arg_index: usize, errors: &mut TokenStream2) -> Option { if let Some(FnArg::Typed(arg)) = f.sig.inputs.iter().nth(arg_index) { diff --git a/uefi-macros/tests/compilation.rs b/uefi-macros/tests/compilation.rs deleted file mode 100644 index 870c2f95e..000000000 --- a/uefi-macros/tests/compilation.rs +++ /dev/null @@ -1,5 +0,0 @@ -#[test] -fn ui() { - let t = trybuild::TestCases::new(); - t.compile_fail("tests/ui/*.rs"); -} diff --git a/uefi-macros/tests/ui/entry_bad_abi.rs b/uefi-macros/tests/ui/entry_bad_abi.rs deleted file mode 100644 index 5c43cd273..000000000 --- a/uefi-macros/tests/ui/entry_bad_abi.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -extern "C" fn main(_handle: Handle, _st: SystemTable) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_bad_abi.stderr b/uefi-macros/tests/ui/entry_bad_abi.stderr deleted file mode 100644 index a40d65a88..000000000 --- a/uefi-macros/tests/ui/entry_bad_abi.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Entry method must have no ABI modifier - --> tests/ui/entry_bad_abi.rs:8:1 - | -8 | extern "C" fn main(_handle: Handle, _st: SystemTable) -> Status { - | ^^^^^^ diff --git a/uefi-macros/tests/ui/entry_bad_arg.rs b/uefi-macros/tests/ui/entry_bad_arg.rs deleted file mode 100644 index 8bd8f2453..000000000 --- a/uefi-macros/tests/ui/entry_bad_arg.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -fn main(_handle: Handle, _st: SystemTable, _x: usize) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_bad_arg.stderr b/uefi-macros/tests/ui/entry_bad_arg.stderr deleted file mode 100644 index 437826fd8..000000000 --- a/uefi-macros/tests/ui/entry_bad_arg.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error[E0308]: mismatched types - --> tests/ui/entry_bad_arg.rs:8:1 - | -8 | fn main(_handle: Handle, _st: SystemTable, _x: usize) -> Status { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ incorrect number of function parameters - | - = note: expected fn pointer `extern "efiapi" fn(uefi::Handle, uefi::table::SystemTable) -> uefi::Status` - found fn pointer `extern "efiapi" fn(uefi::Handle, uefi::table::SystemTable, usize) -> uefi::Status` diff --git a/uefi-macros/tests/ui/entry_bad_async.rs b/uefi-macros/tests/ui/entry_bad_async.rs deleted file mode 100644 index d13125eb2..000000000 --- a/uefi-macros/tests/ui/entry_bad_async.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -async fn main(_handle: Handle, _st: SystemTable) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_bad_async.stderr b/uefi-macros/tests/ui/entry_bad_async.stderr deleted file mode 100644 index c2fe0df9e..000000000 --- a/uefi-macros/tests/ui/entry_bad_async.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Entry method should not be async - --> tests/ui/entry_bad_async.rs:8:1 - | -8 | async fn main(_handle: Handle, _st: SystemTable) -> Status { - | ^^^^^ diff --git a/uefi-macros/tests/ui/entry_bad_attr_arg.rs b/uefi-macros/tests/ui/entry_bad_attr_arg.rs deleted file mode 100644 index c9ace76f3..000000000 --- a/uefi-macros/tests/ui/entry_bad_attr_arg.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry(some_arg)] -fn main(_handle: Handle, _st: SystemTable) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_bad_attr_arg.stderr b/uefi-macros/tests/ui/entry_bad_attr_arg.stderr deleted file mode 100644 index fff44a4b5..000000000 --- a/uefi-macros/tests/ui/entry_bad_attr_arg.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Entry attribute accepts no arguments - --> tests/ui/entry_bad_attr_arg.rs:7:9 - | -7 | #[entry(some_arg)] - | ^^^^^^^^ diff --git a/uefi-macros/tests/ui/entry_bad_const.rs b/uefi-macros/tests/ui/entry_bad_const.rs deleted file mode 100644 index f98c33405..000000000 --- a/uefi-macros/tests/ui/entry_bad_const.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -const fn main(_handle: Handle, _st: SystemTable) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_bad_const.stderr b/uefi-macros/tests/ui/entry_bad_const.stderr deleted file mode 100644 index 241724602..000000000 --- a/uefi-macros/tests/ui/entry_bad_const.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Entry method should not be const - --> tests/ui/entry_bad_const.rs:8:1 - | -8 | const fn main(_handle: Handle, _st: SystemTable) -> Status { - | ^^^^^ diff --git a/uefi-macros/tests/ui/entry_bad_generic.rs b/uefi-macros/tests/ui/entry_bad_generic.rs deleted file mode 100644 index e1ad5fcc1..000000000 --- a/uefi-macros/tests/ui/entry_bad_generic.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -fn main(_handle: Handle, _st: SystemTable) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_bad_generic.stderr b/uefi-macros/tests/ui/entry_bad_generic.stderr deleted file mode 100644 index 9fd0e708e..000000000 --- a/uefi-macros/tests/ui/entry_bad_generic.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Entry method should not be generic - --> tests/ui/entry_bad_generic.rs:8:9 - | -8 | fn main(_handle: Handle, _st: SystemTable) -> Status { - | ^ diff --git a/uefi-macros/tests/ui/entry_bad_return_type.rs b/uefi-macros/tests/ui/entry_bad_return_type.rs deleted file mode 100644 index e2ac39960..000000000 --- a/uefi-macros/tests/ui/entry_bad_return_type.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -fn main(_handle: Handle, _st: SystemTable) -> bool { - false -} diff --git a/uefi-macros/tests/ui/entry_bad_return_type.stderr b/uefi-macros/tests/ui/entry_bad_return_type.stderr deleted file mode 100644 index 3cb1206b5..000000000 --- a/uefi-macros/tests/ui/entry_bad_return_type.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error[E0308]: mismatched types - --> tests/ui/entry_bad_return_type.rs:8:1 - | -8 | fn main(_handle: Handle, _st: SystemTable) -> bool { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Status`, found `bool` - | - = note: expected fn pointer `extern "efiapi" fn(uefi::Handle, uefi::table::SystemTable<_>) -> Status` - found fn pointer `extern "efiapi" fn(uefi::Handle, uefi::table::SystemTable<_>) -> bool` diff --git a/uefi-macros/tests/ui/entry_unnamed_image_arg.rs b/uefi-macros/tests/ui/entry_unnamed_image_arg.rs deleted file mode 100644 index 13cd255e1..000000000 --- a/uefi-macros/tests/ui/entry_unnamed_image_arg.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -fn unnamed_image_arg(_: Handle, _st: SystemTable) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_unnamed_image_arg.stderr b/uefi-macros/tests/ui/entry_unnamed_image_arg.stderr deleted file mode 100644 index f8014bf94..000000000 --- a/uefi-macros/tests/ui/entry_unnamed_image_arg.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Entry method's arguments must be named - --> tests/ui/entry_unnamed_image_arg.rs:8:22 - | -8 | fn unnamed_image_arg(_: Handle, _st: SystemTable) -> Status { - | ^ diff --git a/uefi-macros/tests/ui/entry_unnamed_table_arg.rs b/uefi-macros/tests/ui/entry_unnamed_table_arg.rs deleted file mode 100644 index 233bffca4..000000000 --- a/uefi-macros/tests/ui/entry_unnamed_table_arg.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![allow(unused_imports)] -#![no_main] - -use uefi::prelude::*; -use uefi_macros::entry; - -#[entry] -fn unnamed_table_arg(_image: Handle, _: SystemTable) -> Status { - Status::SUCCESS -} diff --git a/uefi-macros/tests/ui/entry_unnamed_table_arg.stderr b/uefi-macros/tests/ui/entry_unnamed_table_arg.stderr deleted file mode 100644 index a7138c6e7..000000000 --- a/uefi-macros/tests/ui/entry_unnamed_table_arg.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: Entry method's arguments must be named - --> tests/ui/entry_unnamed_table_arg.rs:8:38 - | -8 | fn unnamed_table_arg(_image: Handle, _: SystemTable) -> Status { - | ^ diff --git a/uefi-macros/tests/ui/guid_bad_hex_group2.rs b/uefi-macros/tests/ui/guid_bad_hex_group2.rs deleted file mode 100644 index c13466974..000000000 --- a/uefi-macros/tests/ui/guid_bad_hex_group2.rs +++ /dev/null @@ -1,7 +0,0 @@ -use uefi::Guid; -use uefi_macros::guid; - -// Error span should point to the second group. -const BadHexGroup2: Guid = guid!("aaaaaaaa-Gaaa-aaaa-aaaa-aaaaaaaaaaaa"); - -fn main() {} diff --git a/uefi-macros/tests/ui/guid_bad_hex_group2.stderr b/uefi-macros/tests/ui/guid_bad_hex_group2.stderr deleted file mode 100644 index ca59b447d..000000000 --- a/uefi-macros/tests/ui/guid_bad_hex_group2.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: GUID component "Gaaa" is not a hexadecimal number - --> tests/ui/guid_bad_hex_group2.rs:5:34 - | -5 | const BadHexGroup2: Guid = guid!("aaaaaaaa-Gaaa-aaaa-aaaa-aaaaaaaaaaaa"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uefi-macros/tests/ui/guid_bad_hex_group5.rs b/uefi-macros/tests/ui/guid_bad_hex_group5.rs deleted file mode 100644 index 928fb382c..000000000 --- a/uefi-macros/tests/ui/guid_bad_hex_group5.rs +++ /dev/null @@ -1,7 +0,0 @@ -use uefi::Guid; -use uefi_macros::guid; - -// Error span should point to the fifth group. -const BadHexGroup5: Guid = guid!("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaG"); - -fn main() {} diff --git a/uefi-macros/tests/ui/guid_bad_hex_group5.stderr b/uefi-macros/tests/ui/guid_bad_hex_group5.stderr deleted file mode 100644 index 36ba8343b..000000000 --- a/uefi-macros/tests/ui/guid_bad_hex_group5.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: GUID component "aaaaaaaaaaaG" is not a hexadecimal number - --> tests/ui/guid_bad_hex_group5.rs:5:34 - | -5 | const BadHexGroup5: Guid = guid!("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaG"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uefi-macros/tests/ui/guid_bad_length.rs b/uefi-macros/tests/ui/guid_bad_length.rs deleted file mode 100644 index 0d70ff34a..000000000 --- a/uefi-macros/tests/ui/guid_bad_length.rs +++ /dev/null @@ -1,7 +0,0 @@ -use uefi::Guid; -use uefi_macros::guid; - -// Fail because the length is wrong. -const TooShort: Guid = guid!("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa"); - -fn main() {} diff --git a/uefi-macros/tests/ui/guid_bad_length.stderr b/uefi-macros/tests/ui/guid_bad_length.stderr deleted file mode 100644 index 77fbfb790..000000000 --- a/uefi-macros/tests/ui/guid_bad_length.stderr +++ /dev/null @@ -1,5 +0,0 @@ -error: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa" is not a canonical GUID string (expected 36 bytes, found 35) - --> tests/ui/guid_bad_length.rs:5:30 - | -5 | const TooShort: Guid = guid!("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa"); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/uefi/Cargo.toml b/uefi/Cargo.toml index 1b0b7ad28..9e0b803a3 100644 --- a/uefi/Cargo.toml +++ b/uefi/Cargo.toml @@ -31,6 +31,7 @@ ptr_meta = { version = "0.2.0", default-features = false } ucs2 = "0.3.2" uefi-macros = "0.11.0" uefi-raw = "0.1.0" +uguid = "2.0.0" [package.metadata.docs.rs] all-features = true diff --git a/uefi/src/data_types/guid.rs b/uefi/src/data_types/guid.rs index ef8800b83..6fe695737 100644 --- a/uefi/src/data_types/guid.rs +++ b/uefi/src/data_types/guid.rs @@ -1,114 +1,4 @@ -use core::fmt; - -/// A globally unique identifier -/// -/// GUIDs are used by UEFI to identify protocols and other objects. They are -/// mostly like variant 2 UUIDs as specified by RFC 4122, but differ from them -/// in that the first 3 fields are little endian instead of big endian. -/// -/// The `Display` formatter prints GUIDs in the canonical format defined by -/// RFC 4122, which is also used by UEFI. -#[derive(Debug, Default, Copy, Clone, Eq, Ord, PartialEq, PartialOrd)] -#[repr(C)] -pub struct Guid { - /// The low field of the timestamp. - a: u32, - /// The middle field of the timestamp. - b: u16, - /// The high field of the timestamp multiplexed with the version number. - c: u16, - /// Contains, in this order: - /// - The high field of the clock sequence multiplexed with the variant. - /// - The low field of the clock sequence. - /// - The spatially unique node identifier. - d: [u8; 8], -} - -impl Guid { - /// Creates a new GUID from its canonical representation - #[must_use] - pub const fn from_values( - time_low: u32, - time_mid: u16, - time_high_and_version: u16, - clock_seq_and_variant: u16, - node: u64, - ) -> Self { - assert!(node.leading_zeros() >= 16, "node must be a 48-bit integer"); - // intentional shadowing - let node = node.to_be_bytes(); - - Guid { - a: time_low, - b: time_mid, - c: time_high_and_version, - d: [ - (clock_seq_and_variant / 0x100) as u8, - (clock_seq_and_variant % 0x100) as u8, - // first two elements of node are ignored, we only want the low 48 bits - node[2], - node[3], - node[4], - node[5], - node[6], - node[7], - ], - } - } - - /// Create a GUID from a 16-byte array. No changes to byte order are made. - #[must_use] - pub const fn from_bytes(bytes: [u8; 16]) -> Self { - let a = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]); - let b = u16::from_le_bytes([bytes[4], bytes[5]]); - let c = u16::from_le_bytes([bytes[6], bytes[7]]); - let d = [ - bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15], - ]; - - Self { a, b, c, d } - } - - /// Convert to a 16-byte array. - #[must_use] - #[rustfmt::skip] - pub const fn to_bytes(self) -> [u8; 16] { - let a = self.a.to_le_bytes(); - let b = self.b.to_le_bytes(); - let c = self.c.to_le_bytes(); - let d = self.d; - - [ - a[0], a[1], a[2], a[3], - b[0], b[1], c[0], c[1], - d[0], d[1], d[2], d[3], - d[4], d[5], d[6], d[7], - ] - } -} - -impl fmt::Display for Guid { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let a = self.a; - let b = self.b; - let c = self.c; - - let d = { - let mut buf = [0u8; 2]; - buf[..].copy_from_slice(&self.d[0..2]); - u16::from_be_bytes(buf) - }; - - let e = { - let mut buf = [0u8; 8]; - // first two elements of node are ignored, we only want the low 48 bits - buf[2..].copy_from_slice(&self.d[2..8]); - u64::from_be_bytes(buf) - }; - - write!(fmt, "{a:08x}-{b:04x}-{c:04x}-{d:04x}-{e:012x}",) - } -} +pub use uguid::Guid; /// Several entities in the UEFI specification can be referred to by their GUID, /// this trait is a building block to interface them in uefi-rs. @@ -132,45 +22,3 @@ pub unsafe trait Identify { /// Unique protocol identifier. const GUID: Guid; } - -#[cfg(test)] -mod tests { - use super::*; - use crate::guid; - - #[test] - fn test_guid_display() { - assert_eq!( - alloc::format!( - "{}", - Guid::from_values(0x12345678, 0x9abc, 0xdef0, 0x1234, 0x56789abcdef0) - ), - "12345678-9abc-def0-1234-56789abcdef0" - ); - } - - #[test] - fn test_guid_macro() { - assert_eq!( - guid!("12345678-9abc-def0-1234-56789abcdef0"), - Guid::from_values(0x12345678, 0x9abc, 0xdef0, 0x1234, 0x56789abcdef0) - ); - } - - #[test] - fn test_to_from_bytes() { - #[rustfmt::skip] - let bytes = [ - 0x78, 0x56, 0x34, 0x12, - 0xbc, 0x9a, - 0xf0, 0xde, - 0x12, 0x34, - 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, - ]; - assert_eq!( - Guid::from_bytes(bytes), - Guid::from_values(0x12345678, 0x9abc, 0xdef0, 0x1234, 0x56789abcdef0) - ); - assert_eq!(Guid::from_bytes(bytes).to_bytes(), bytes); - } -} diff --git a/uefi/src/lib.rs b/uefi/src/lib.rs index 5f8f8d9ab..1c5499658 100644 --- a/uefi/src/lib.rs +++ b/uefi/src/lib.rs @@ -100,7 +100,8 @@ pub mod data_types; #[cfg(feature = "alloc")] pub use self::data_types::CString16; pub use self::data_types::{CStr16, CStr8, Char16, Char8, Event, Guid, Handle, Identify}; -pub use uefi_macros::{cstr16, cstr8, entry, guid}; +pub use uefi_macros::{cstr16, cstr8, entry}; +pub use uguid::guid; mod result; pub use self::result::{Error, Result, ResultExt, Status, StatusExt};