#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![allow(clippy::cast_possible_truncation)]
#![allow(unknown_lints)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unsafe_op_in_unsafe_fn)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_alias))]
#![cfg_attr(
feature = "bytes",
doc = "- [`bytes::SymbolTable`] interns binary strings: [`Vec<u8>`] and `&[u8]`."
)]
#![cfg_attr(
feature = "cstr",
doc = "- [`cstr::SymbolTable`] interns C strings: [`CString`] and [`&CStr`]."
)]
#![cfg_attr(
feature = "osstr",
doc = "- [`osstr::SymbolTable`] interns platform strings: [`OsString`] and [`&OsStr`]."
)]
#![cfg_attr(
feature = "path",
doc = "- [`path::SymbolTable`] interns path strings: [`PathBuf`] and [`&Path`]."
)]
#![doc(html_root_url = "https://docs.rs/intaglio/1.8.0")]
use core::fmt;
use core::num::TryFromIntError;
use std::error;
macro_rules! const_assert {
($x:expr $(,)?) => {
#[allow(unknown_lints, clippy::eq_op)]
const _: [(); 0 - !{
const ASSERT: bool = $x;
ASSERT
} as usize] = [];
};
}
#[cfg(feature = "bytes")]
#[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
pub mod bytes;
mod convert;
#[cfg(feature = "cstr")]
#[cfg_attr(docsrs, doc(cfg(feature = "cstr")))]
pub mod cstr;
mod eq;
mod internal;
#[cfg(feature = "osstr")]
#[cfg_attr(docsrs, doc(cfg(feature = "osstr")))]
pub mod osstr;
#[cfg(feature = "path")]
#[cfg_attr(docsrs, doc(cfg(feature = "path")))]
pub mod path;
mod str;
pub use crate::str::*;
const_assert!(usize::BITS >= u32::BITS);
pub const DEFAULT_SYMBOL_TABLE_CAPACITY: usize = 4096;
#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct SymbolOverflowError {
_private: (),
}
impl SymbolOverflowError {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self { _private: () }
}
#[inline]
#[must_use]
#[allow(clippy::unused_self)]
pub const fn max_capacity(self) -> usize {
let capa = u32::MAX as usize;
capa.saturating_add(1)
}
}
impl From<TryFromIntError> for SymbolOverflowError {
#[inline]
fn from(err: TryFromIntError) -> Self {
let _ = err;
Self::new()
}
}
impl fmt::Display for SymbolOverflowError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Symbol overflow")
}
}
impl error::Error for SymbolOverflowError {}
#[repr(transparent)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Symbol(u32);
impl Symbol {
#[inline]
#[must_use]
pub const fn new(sym: u32) -> Self {
Self(sym)
}
#[inline]
#[must_use]
pub const fn id(self) -> u32 {
self.0
}
}
#[cfg(test)]
mod tests {
use core::cmp::Ordering;
use core::fmt::Write as _;
use core::hash::{BuildHasher as _, Hash as _, Hasher as _};
use std::collections::hash_map::RandomState;
use super::SymbolOverflowError;
#[test]
#[cfg(target_pointer_width = "64")]
fn max_capacity_is_length_of_symbol_range_usize_64_bit() {
let symbol_range = 0_u32..=u32::MAX;
let len = symbol_range.size_hint().0;
assert_eq!(SymbolOverflowError::new().max_capacity(), len);
let len = symbol_range.size_hint().1.unwrap();
assert_eq!(SymbolOverflowError::new().max_capacity(), len);
}
#[test]
#[cfg(target_pointer_width = "32")]
fn max_capacity_is_length_of_symbol_range_usize_32_bit() {
assert_eq!(SymbolOverflowError::new().max_capacity(), usize::MAX);
}
#[test]
fn error_display_is_not_empty() {
let tc = SymbolOverflowError::new();
let mut buf = String::new();
write!(&mut buf, "{}", tc).unwrap();
assert!(!buf.is_empty());
}
#[test]
fn error_debug_is_not_empty() {
let tc = SymbolOverflowError::new();
let mut buf = String::new();
write!(&mut buf, "{:?}", tc).unwrap();
assert!(!buf.is_empty());
}
#[test]
fn error_from_int_conversion_error() {
let try_from_int_error = i8::try_from(u8::MAX).unwrap_err();
let err = SymbolOverflowError::from(try_from_int_error);
assert_eq!(err, SymbolOverflowError::new());
}
#[test]
fn error_default_is_error_new() {
let default = SymbolOverflowError::default();
let new = SymbolOverflowError::new();
assert_eq!(default, new);
}
#[test]
fn error_clone_is_equal_to_self() {
let default = SymbolOverflowError::default();
#[allow(clippy::clone_on_copy)]
let clone = default.clone();
assert_eq!(default, clone);
}
#[test]
fn error_ord_is_equal_to_self() {
let default = SymbolOverflowError::default();
let new = SymbolOverflowError::new();
assert_eq!(default.cmp(&new), Ordering::Equal);
assert_eq!(new.cmp(&default), Ordering::Equal);
}
#[test]
fn error_hash_is_equal_to_self() {
let default = SymbolOverflowError::default();
let new = SymbolOverflowError::new();
let s = RandomState::new();
let default_hash = {
let mut hasher = s.build_hasher();
default.hash(&mut hasher);
hasher.finish()
};
let new_hash = {
let mut hasher = s.build_hasher();
new.hash(&mut hasher);
hasher.finish()
};
assert_eq!(default_hash, new_hash);
}
}
#[cfg(all(
doctest,
feature = "bytes",
feature = "cstr",
feature = "osstr",
feature = "path"
))]
#[doc = include_str!("../README.md")]
mod readme {}