jid: Make the crate no_std

Emmanuel Gil Peyrot created

Only std::error::Error is missing in this mode, but thatโ€™s only waiting
for its stabilisation, see this issue:
https://github.com/rust-lang/rust/issues/103765

Change summary

jid/Cargo.toml   |  2 ++
jid/src/error.rs |  4 +++-
jid/src/lib.rs   | 33 ++++++++++++++++++++++-----------
jid/src/parts.rs | 13 ++++++++-----
4 files changed, 35 insertions(+), 17 deletions(-)

Detailed changes

jid/Cargo.toml ๐Ÿ”—

@@ -32,4 +32,6 @@ serde_test = "1"
 jid = { path = ".", features = [ "serde" ] }
 
 [features]
+default = ["std"]
+std = []
 quote = ["dep:quote", "dep:proc-macro2"]

jid/src/error.rs ๐Ÿ”—

@@ -8,8 +8,9 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+use core::fmt;
+#[cfg(feature = "std")]
 use std::error::Error as StdError;
-use std::fmt;
 
 /// An error that signifies that a `Jid` cannot be parsed from a string.
 #[derive(Debug, PartialEq, Eq)]
@@ -49,6 +50,7 @@ pub enum Error {
     ResourceInBareJid,
 }
 
+#[cfg(feature = "std")]
 impl StdError for Error {}
 
 impl fmt::Display for Error {

jid/src/lib.rs ๐Ÿ”—

@@ -8,6 +8,7 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+#![no_std]
 #![deny(missing_docs)]
 
 //! Represents XMPP addresses, also known as JabberIDs (JIDs) for the [XMPP](https://xmpp.org/)
@@ -30,13 +31,22 @@
 //! - stringprep error: some characters were invalid according to the stringprep algorithm, such as
 //!   mixing left-to-write and right-to-left characters
 
+extern crate alloc;
+
+#[cfg(any(feature = "std", test))]
+extern crate std;
+
+use alloc::borrow::Cow;
+use alloc::format;
+use alloc::string::{String, ToString};
+use core::borrow::Borrow;
+use core::cmp::Ordering;
+use core::fmt;
+use core::hash::{Hash, Hasher};
+use core::mem;
 use core::num::NonZeroU16;
-use std::borrow::{Borrow, Cow};
-use std::cmp::Ordering;
-use std::fmt;
-use std::hash::{Hash, Hasher};
-use std::ops::Deref;
-use std::str::FromStr;
+use core::ops::Deref;
+use core::str::FromStr;
 
 use memchr::memchr;
 
@@ -364,13 +374,13 @@ impl Jid {
             Ok(unsafe {
                 // SAFETY: FullJid is #[repr(transparent)] of Jid
                 // SOUNDNESS: we asserted that self.slash is set above
-                std::mem::transmute::<&Jid, &FullJid>(self)
+                mem::transmute::<&Jid, &FullJid>(self)
             })
         } else {
             Err(unsafe {
                 // SAFETY: BareJid is #[repr(transparent)] of Jid
                 // SOUNDNESS: we asserted that self.slash is unset above
-                std::mem::transmute::<&Jid, &BareJid>(self)
+                mem::transmute::<&Jid, &BareJid>(self)
             })
         }
     }
@@ -383,13 +393,13 @@ impl Jid {
             Ok(unsafe {
                 // SAFETY: FullJid is #[repr(transparent)] of Jid
                 // SOUNDNESS: we asserted that self.slash is set above
-                std::mem::transmute::<&mut Jid, &mut FullJid>(self)
+                mem::transmute::<&mut Jid, &mut FullJid>(self)
             })
         } else {
             Err(unsafe {
                 // SAFETY: BareJid is #[repr(transparent)] of Jid
                 // SOUNDNESS: we asserted that self.slash is unset above
-                std::mem::transmute::<&mut Jid, &mut BareJid>(self)
+                mem::transmute::<&mut Jid, &mut BareJid>(self)
             })
         }
     }
@@ -865,10 +875,11 @@ mod tests {
     use super::*;
 
     use std::collections::{HashMap, HashSet};
+    use std::vec::Vec;
 
     macro_rules! assert_size (
         ($t:ty, $sz:expr) => (
-            assert_eq!(::std::mem::size_of::<$t>(), $sz);
+            assert_eq!(::core::mem::size_of::<$t>(), $sz);
         );
     );
 

jid/src/parts.rs ๐Ÿ”—

@@ -1,7 +1,10 @@
-use std::borrow::{Borrow, Cow};
-use std::fmt;
-use std::ops::Deref;
-use std::str::FromStr;
+use alloc::borrow::{Cow, ToOwned};
+use alloc::string::{String, ToString};
+use core::borrow::Borrow;
+use core::fmt;
+use core::mem;
+use core::ops::Deref;
+use core::str::FromStr;
 
 use stringprep::{nameprep, nodeprep, resourceprep};
 
@@ -162,7 +165,7 @@ macro_rules! def_part_types {
             pub(crate) fn from_str_unchecked(s: &str) -> &Self {
                 // SAFETY: repr(transparent) thing can be transmuted to/from
                 // its inner.
-                unsafe { std::mem::transmute(s) }
+                unsafe { mem::transmute(s) }
             }
 
             /// Access the contents as [`str`] slice.