From 48f77acb690c025952f385a58f7850e5c63401e4 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 25 Apr 2024 21:18:39 +0200 Subject: [PATCH] jid: Make the crate no_std MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- 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(-) diff --git a/jid/Cargo.toml b/jid/Cargo.toml index ba5b97d3..98588df7 100644 --- a/jid/Cargo.toml +++ b/jid/Cargo.toml @@ -32,4 +32,6 @@ serde_test = "1" jid = { path = ".", features = [ "serde" ] } [features] +default = ["std"] +std = [] quote = ["dep:quote", "dep:proc-macro2"] diff --git a/jid/src/error.rs b/jid/src/error.rs index 9422efd4..a954931a 100644 --- a/jid/src/error.rs +++ b/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 { diff --git a/jid/src/lib.rs b/jid/src/lib.rs index 15d22229..18e1133e 100644 --- a/jid/src/lib.rs +++ b/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); ); ); diff --git a/jid/src/parts.rs b/jid/src/parts.rs index c4b56436..ab9ae98a 100644 --- a/jid/src/parts.rs +++ b/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.