diff --git a/xso-proc/src/enums.rs b/xso-proc/src/enums.rs index 54dcef9502ee32ae15802e09cc90c7ca45d0ce1b..35bbdae0034b2668f65bebe54cd1110c1f22201c 100644 --- a/xso-proc/src/enums.rs +++ b/xso-proc/src/enums.rs @@ -140,7 +140,7 @@ impl NameVariant { quote! { let name = ( ::xso::exports::rxml::Namespace::from(#xml_namespace), - ::std::borrow::Cow::Borrowed(#xml_name), + ::xso::exports::alloc::borrow::Cow::Borrowed(#xml_name), ); #init } diff --git a/xso-proc/src/field/attribute.rs b/xso-proc/src/field/attribute.rs index f9d15456540527d1a06654409fb9fcfce58522cb..47ab601f826cb40d22dfea35f070affde66fdf20 100644 --- a/xso-proc/src/field/attribute.rs +++ b/xso-proc/src/field/attribute.rs @@ -128,7 +128,7 @@ impl Field for AttributeField { generator: quote! { #generator.map(|#bound_name| ::xso::Item::Attribute( #xml_namespace, - ::std::borrow::Cow::Borrowed(#xml_name), + ::xso::exports::alloc::borrow::Cow::Borrowed(#xml_name), #bound_name, )); }, diff --git a/xso-proc/src/field/child.rs b/xso-proc/src/field/child.rs index 82681a73f4272001b3cc38078f16355db793d24e..0710f3d22c9775b414ab50249b3bd51c89940246 100644 --- a/xso-proc/src/field/child.rs +++ b/xso-proc/src/field/child.rs @@ -423,7 +423,7 @@ impl ExtractDef { quote! { let name = ( ::xso::exports::rxml::Namespace::from(#xml_namespace), - ::std::borrow::Cow::Borrowed(#xml_name), + ::xso::exports::alloc::borrow::Cow::Borrowed(#xml_name), ); #init } diff --git a/xso-proc/src/field/flag.rs b/xso-proc/src/field/flag.rs index 77dd122fa0a68c17ef50c18dd93ea97516892f6f..4e1789be378134d9dd9f4ebbae8e05faa16ee572 100644 --- a/xso-proc/src/field/flag.rs +++ b/xso-proc/src/field/flag.rs @@ -125,7 +125,7 @@ impl Field for FlagField { 2 => ::core::result::Result::Ok(::core::option::Option::Some( ::xso::Item::ElementHeadStart( ::xso::exports::rxml::Namespace::from(#xml_namespace), - ::std::borrow::Cow::Borrowed(#xml_name), + ::xso::exports::alloc::borrow::Cow::Borrowed(#xml_name), ) )), _ => unreachable!(), diff --git a/xso-proc/src/field/text.rs b/xso-proc/src/field/text.rs index 9e14d0ad482612b90b2dd78663554b1b69461f8d..88aa8e448f0d90626de5b45b967cb716bad5bcda 100644 --- a/xso-proc/src/field/text.rs +++ b/xso-proc/src/field/text.rs @@ -51,7 +51,7 @@ impl Field for TextField { Ok(FieldBuilderPart::Text { value: FieldTempInit { - init: quote! { ::std::string::String::new() }, + init: quote! { ::xso::exports::alloc::string::String::new() }, ty: string_ty(Span::call_site()), }, collect: quote! { diff --git a/xso-proc/src/structs.rs b/xso-proc/src/structs.rs index e6ce621cd077fbf6956922b64b7d9cf55c771db0..f625459cd972a6ae53c802af930c34c7e1fe780f 100644 --- a/xso-proc/src/structs.rs +++ b/xso-proc/src/structs.rs @@ -294,7 +294,7 @@ impl StructInner { quote! { let name = ( ::xso::exports::rxml::Namespace::from(#xml_namespace), - ::std::borrow::Cow::Borrowed(#xml_name), + ::xso::exports::alloc::borrow::Cow::Borrowed(#xml_name), ); #init } diff --git a/xso-proc/src/types.rs b/xso-proc/src/types.rs index ad6901a77c1bde7d001fc2b7dd4523e471a94481..938f84884d793b8bf856a5090cb2bdee2bd9b9d4 100644 --- a/xso-proc/src/types.rs +++ b/xso-proc/src/types.rs @@ -84,7 +84,15 @@ pub(crate) fn cow_ty(ty: Type, lifetime: Lifetime) -> Type { }), segments: [ PathSegment { - ident: Ident::new("std", span), + ident: Ident::new("xso", span), + arguments: PathArguments::None, + }, + PathSegment { + ident: Ident::new("exports", span), + arguments: PathArguments::None, + }, + PathSegment { + ident: Ident::new("alloc", span), arguments: PathArguments::None, }, PathSegment { @@ -233,7 +241,7 @@ pub(crate) fn default_fn(of_ty: Type) -> Expr { }) } -/// Construct a [`syn::Type`] referring to `::std::string::String`. +/// Construct a [`syn::Type`] referring to `::alloc::string::String`. pub(crate) fn string_ty(span: Span) -> Type { Type::Path(TypePath { qself: None, @@ -243,7 +251,15 @@ pub(crate) fn string_ty(span: Span) -> Type { }), segments: [ PathSegment { - ident: Ident::new("std", span), + ident: Ident::new("xso", span), + arguments: PathArguments::None, + }, + PathSegment { + ident: Ident::new("exports", span), + arguments: PathArguments::None, + }, + PathSegment { + ident: Ident::new("alloc", span), arguments: PathArguments::None, }, PathSegment { diff --git a/xso/ChangeLog b/xso/ChangeLog index a72c16aa24c028454997e2a15b7f299062e78a1f..b9a527fa67e1f0f9a42d0c63a2d2db4f272e18df 100644 --- a/xso/ChangeLog +++ b/xso/ChangeLog @@ -31,6 +31,7 @@ Version NEXT: the base64 crate (if the `base64` feature is enabled). - New `codec` field on `attribute` meta, to support decoding and encoding using any `TextCodec`. + - Support for `no_std` usage (the alloc crate is required, though). Version 0.1.2: 2024-07-26 Jonas Schäfer diff --git a/xso/src/lib.rs b/xso/src/lib.rs index 5fee3065654fe31e3b258989c65c768544452db3..ceb5929ec6f5ded12ae51c96c5a34f0e4cf31b06 100644 --- a/xso/src/lib.rs +++ b/xso/src/lib.rs @@ -45,6 +45,22 @@ pub mod exports { pub use minidom; pub use rxml; + // These re-exports are necessary to support both std and no_std in code + // generated by the macros. + // + // If we attempted to use ::alloc directly from macros, std builds would + // not work because alloc is not generally present in builds using std. + // If we used ::std, no_std builds would obviously not work. By exporting + // std as alloc in std builds, we can safely use the alloc types from + // there. + // + // Obviously, we have to be careful in xso-proc to not refer to types + // which are not in alloc. + #[cfg(not(feature = "std"))] + pub extern crate alloc; + #[cfg(feature = "std")] + pub extern crate std as alloc; + /// The built-in `bool` type. /// /// This is re-exported for use by macros in cases where we cannot rely on