Skip to content

Commit

Permalink
jid: Make the crate no_std
Browse files Browse the repository at this point in the history
Only std::error::Error is missing in this mode, but that’s only waiting
for its stabilisation, see this issue:
rust-lang/rust#103765
  • Loading branch information
linkmauve authored and Ppjet6 committed May 4, 2024
1 parent fcadccf commit 48f77ac
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
2 changes: 2 additions & 0 deletions jid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ serde_test = "1"
jid = { path = ".", features = [ "serde" ] }

[features]
default = ["std"]
std = []
quote = ["dep:quote", "dep:proc-macro2"]
4 changes: 3 additions & 1 deletion jid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -49,6 +50,7 @@ pub enum Error {
ResourceInBareJid,
}

#[cfg(feature = "std")]
impl StdError for Error {}

impl fmt::Display for Error {
Expand Down
33 changes: 22 additions & 11 deletions jid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand All @@ -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;

Expand Down Expand Up @@ -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)
})
}
}
Expand All @@ -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)
})
}
}
Expand Down Expand Up @@ -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);
);
);

Expand Down
13 changes: 8 additions & 5 deletions jid/src/parts.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 48f77ac

Please sign in to comment.