Skip to content

Commit 1ab4840

Browse files
authored
Merge pull request #777 from nicholasbishop/bishop-uguid-3
Use the uguid crate to replace the `Guid` struct and `guid!` macro
2 parents 1944acc + 5894e51 commit 1ab4840

32 files changed

+19
-485
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
`Status`. `impl From<Status> for Result` has also been removed. A new
3636
`StatusExt` trait has been added that provides conversion methods to replace
3737
the ones that have been removed. `StatusExt` has been added to the prelude.
38+
- The `Guid` struct and `guid!` macro implementations have been replaced with
39+
re-exports from the [`uguid`](https://docs.rs/uguid) crate. The `from_values`
40+
method has been removed; usually the `guid!` macro is a more convenient
41+
choice, but `new` or `from_bytes` can also be used if needed. There are also a
42+
number of new `Guid` methods.
3843

3944
## uefi-macros - [Unreleased]
4045

Cargo.lock

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

uefi-macros/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ quote = "1.0.9"
2020
syn = { version = "2.0.4", features = ["full"] }
2121

2222
[dev-dependencies]
23-
trybuild = "1.0.61"
2423
uefi = { version = "0.20.0", default-features = false }

uefi-macros/src/lib.rs

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ extern crate proc_macro;
44

55
use proc_macro::TokenStream;
66

7-
use proc_macro2::{TokenStream as TokenStream2, TokenTree};
8-
use quote::{quote, quote_spanned, ToTokens, TokenStreamExt};
7+
use proc_macro2::TokenStream as TokenStream2;
8+
use quote::{quote, quote_spanned, TokenStreamExt};
99
use syn::spanned::Spanned;
1010
use syn::{
1111
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 {
6464
Expr::Lit(ExprLit {
6565
lit: Lit::Str(lit), ..
6666
}) => {
67-
// Parse as a GUID string.
68-
let (time_low, time_mid, time_high_and_version, clock_seq_and_variant, node) =
69-
match parse_guid(lit) {
70-
Ok(data) => data,
71-
Err(tokens) => return tokens.into(),
72-
};
73-
quote!(::uefi::Guid::from_values(
74-
#time_low,
75-
#time_mid,
76-
#time_high_and_version,
77-
#clock_seq_and_variant,
78-
#node,
79-
))
67+
quote!(::uefi::guid!(#lit))
8068
}
8169
Expr::Path(ExprPath { path, .. }) => quote!(#path),
8270
_ => {
@@ -111,90 +99,6 @@ pub fn unsafe_protocol(args: TokenStream, input: TokenStream) -> TokenStream {
11199
.into()
112100
}
113101

114-
/// Create a `Guid` at compile time.
115-
///
116-
/// # Example
117-
///
118-
/// ```
119-
/// use uefi::{guid, Guid};
120-
/// const EXAMPLE_GUID: Guid = guid!("12345678-9abc-def0-1234-56789abcdef0");
121-
/// ```
122-
#[proc_macro]
123-
pub fn guid(args: TokenStream) -> TokenStream {
124-
let (time_low, time_mid, time_high_and_version, clock_seq_and_variant, node) =
125-
match parse_guid(parse_macro_input!(args as LitStr)) {
126-
Ok(data) => data,
127-
Err(tokens) => return tokens.into(),
128-
};
129-
130-
quote!({
131-
const g: ::uefi::Guid = ::uefi::Guid::from_values(
132-
#time_low,
133-
#time_mid,
134-
#time_high_and_version,
135-
#clock_seq_and_variant,
136-
#node,
137-
);
138-
g
139-
})
140-
.into()
141-
}
142-
143-
fn parse_guid(guid_lit: LitStr) -> Result<(u32, u16, u16, u16, u64), TokenStream2> {
144-
let guid_str = guid_lit.value();
145-
146-
// We expect a canonical GUID string, such as "12345678-9abc-def0-fedc-ba9876543210"
147-
if guid_str.len() != 36 {
148-
return Err(err!(
149-
guid_lit,
150-
"\"{}\" is not a canonical GUID string (expected 36 bytes, found {})",
151-
guid_str,
152-
guid_str.len()
153-
));
154-
}
155-
let mut offset = 1; // 1 is for the starting quote
156-
let mut guid_hex_iter = guid_str.split('-');
157-
let mut next_guid_int = |len: usize| -> Result<u64, TokenStream2> {
158-
let guid_hex_component = guid_hex_iter.next().unwrap();
159-
160-
// convert syn::LitStr to proc_macro2::Literal..
161-
let lit = match guid_lit.to_token_stream().into_iter().next().unwrap() {
162-
TokenTree::Literal(lit) => lit,
163-
_ => unreachable!(),
164-
};
165-
// ..so that we can call subspan and nightly users (us) will get the fancy span
166-
let span = lit
167-
.subspan(offset..offset + guid_hex_component.len())
168-
.unwrap_or_else(|| lit.span());
169-
170-
if guid_hex_component.len() != len * 2 {
171-
return Err(err!(
172-
span,
173-
"GUID component \"{}\" is not a {}-bit hexadecimal string",
174-
guid_hex_component,
175-
len * 8
176-
));
177-
}
178-
offset += guid_hex_component.len() + 1; // + 1 for the dash
179-
u64::from_str_radix(guid_hex_component, 16).map_err(|_| {
180-
err!(
181-
span,
182-
"GUID component \"{}\" is not a hexadecimal number",
183-
guid_hex_component
184-
)
185-
})
186-
};
187-
188-
// The GUID string is composed of a 32-bit integer, three 16-bit ones, and a 48-bit one
189-
Ok((
190-
next_guid_int(4)? as u32,
191-
next_guid_int(2)? as u16,
192-
next_guid_int(2)? as u16,
193-
next_guid_int(2)? as u16,
194-
next_guid_int(6)?,
195-
))
196-
}
197-
198102
/// Get the name of a function's argument at `arg_index`.
199103
fn get_function_arg_name(f: &ItemFn, arg_index: usize, errors: &mut TokenStream2) -> Option<Ident> {
200104
if let Some(FnArg::Typed(arg)) = f.sig.inputs.iter().nth(arg_index) {

uefi-macros/tests/compilation.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_abi.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_abi.stderr

Lines changed: 0 additions & 5 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_arg.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_arg.stderr

Lines changed: 0 additions & 8 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_async.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_async.stderr

Lines changed: 0 additions & 5 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_attr_arg.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_attr_arg.stderr

Lines changed: 0 additions & 5 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_const.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_const.stderr

Lines changed: 0 additions & 5 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_generic.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_generic.stderr

Lines changed: 0 additions & 5 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_return_type.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_bad_return_type.stderr

Lines changed: 0 additions & 8 deletions
This file was deleted.

uefi-macros/tests/ui/entry_unnamed_image_arg.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

uefi-macros/tests/ui/entry_unnamed_image_arg.stderr

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)