diff --git a/Cargo.lock b/Cargo.lock index 714ff53ad..ba3a78b5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -458,15 +458,6 @@ dependencies = [ "libc", ] -[[package]] -name = "kstring" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec3066350882a1cd6d950d055997f379ac37fd39f81cd4d8ed186032eb3c5747" -dependencies = [ - "static_assertions", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -841,12 +832,6 @@ dependencies = [ "anstream 0.6.18", ] -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - [[package]] name = "strsim" version = "0.10.0" @@ -1011,7 +996,6 @@ dependencies = [ "anstream 0.6.18", "anstyle", "indexmap 2.3.0", - "kstring", "proptest", "serde", "serde-untagged", diff --git a/crates/benchmarks/Cargo.toml b/crates/benchmarks/Cargo.toml index ac83f486c..4792957ec 100644 --- a/crates/benchmarks/Cargo.toml +++ b/crates/benchmarks/Cargo.toml @@ -11,7 +11,7 @@ release = false [features] default = [] simd = ["toml_parse/simd"] -unsafe = ["toml_parse/unsafe", "toml_edit/unsafe"] +unsafe = ["toml_parse/unsafe"] preserve_order = ["toml_old/preserve_order", "toml/preserve_order"] fast_hash = ["toml/fast_hash"] diff --git a/crates/toml/Cargo.toml b/crates/toml/Cargo.toml index 40176d3d5..44f5d71c9 100644 --- a/crates/toml/Cargo.toml +++ b/crates/toml/Cargo.toml @@ -33,7 +33,6 @@ std = ["indexmap?/std"] serde = ["dep:serde", "toml_datetime/serde", "serde_spanned/serde"] parse = ["dep:toml_parse", "dep:winnow"] display = ["dep:toml_write"] -unsafe = ["toml_parse?/unsafe"] fast_hash = ["preserve_order", "dep:foldhash"] debug = ["std", "toml_parse?/debug", "dep:anstream", "dep:anstyle"] diff --git a/crates/toml/src/de/parser/key.rs b/crates/toml/src/de/parser/key.rs index 6e1c5a853..0152f5053 100644 --- a/crates/toml/src/de/parser/key.rs +++ b/crates/toml/src/de/parser/key.rs @@ -111,9 +111,6 @@ impl State { let key_span = key.span(); let key_span = key_span.start()..key_span.end(); - #[cfg(feature = "unsafe")] // SAFETY: lexing and parsing all with same source - let raw = unsafe { source.get_unchecked(key) }; - #[cfg(not(feature = "unsafe"))] let raw = source.get(key).unwrap(); let mut decoded = alloc::borrow::Cow::Borrowed(""); raw.decode_key(&mut decoded, errors); diff --git a/crates/toml/src/de/parser/value.rs b/crates/toml/src/de/parser/value.rs index cf3935bbb..063cdc767 100644 --- a/crates/toml/src/de/parser/value.rs +++ b/crates/toml/src/de/parser/value.rs @@ -71,9 +71,6 @@ pub(crate) fn on_scalar<'i>( let value_span = event.span(); let value_span = value_span.start()..value_span.end(); - #[cfg(feature = "unsafe")] // SAFETY: lexing and parsing all with same source - let raw = unsafe { source.get_unchecked(event) }; - #[cfg(not(feature = "unsafe"))] let raw = source.get(event).unwrap(); let mut decoded = alloc::borrow::Cow::Borrowed(""); let kind = raw.decode_scalar(&mut decoded, errors); diff --git a/crates/toml/src/lib.rs b/crates/toml/src/lib.rs index f6512da5b..4c210ab81 100644 --- a/crates/toml/src/lib.rs +++ b/crates/toml/src/lib.rs @@ -147,7 +147,7 @@ // Presence of this annotation is picked up by tools such as cargo-geiger // and lets them ensure that there is indeed no unsafe code as opposed to // something they couldn't detect (e.g. unsafe added via macro expansion, etc). -#![cfg_attr(not(feature = "unsafe"), forbid(unsafe_code))] +#![forbid(unsafe_code)] #![warn(missing_docs)] #![warn(clippy::print_stderr)] #![warn(clippy::print_stdout)] diff --git a/crates/toml_edit/Cargo.toml b/crates/toml_edit/Cargo.toml index 102f8a85d..afcd77a97 100644 --- a/crates/toml_edit/Cargo.toml +++ b/crates/toml_edit/Cargo.toml @@ -29,8 +29,6 @@ pre-release-replacements = [ default = ["parse", "display"] parse = ["dep:toml_parse", "dep:winnow"] display = ["dep:toml_write"] -unsafe = ["toml_parse?/unsafe"] -perf = ["dep:kstring", "toml_parse?/simd"] serde = ["dep:serde", "toml_datetime/serde", "dep:serde_spanned"] debug = ["toml_parse?/debug", "dep:anstream", "dep:anstyle", "display"] # Provide a method disable_recursion_limit to parse arbitrarily deep structures @@ -44,7 +42,6 @@ unbounded = [] indexmap = { version = "2.3.0", features = ["std"] } winnow = { version = "0.7.10", optional = true } serde = { version = "1.0.145", optional = true } -kstring = { version = "2.0.0", features = ["max_inline"], optional = true } toml_datetime = { version = "0.6.11", path = "../toml_datetime" } serde_spanned = { version = "0.6.9", path = "../serde_spanned", features = ["serde"], optional = true } toml_write = { version = "0.1.2", path = "../toml_write", optional = true } diff --git a/crates/toml_edit/src/inline_table.rs b/crates/toml_edit/src/inline_table.rs index 866acd8fd..80b324cff 100644 --- a/crates/toml_edit/src/inline_table.rs +++ b/crates/toml_edit/src/inline_table.rs @@ -3,7 +3,7 @@ use std::iter::FromIterator; use crate::key::Key; use crate::repr::Decor; use crate::table::{Iter, IterMut, KeyValuePairs, TableLike}; -use crate::{InternalString, Item, KeyMut, RawString, Table, Value}; +use crate::{Item, KeyMut, RawString, Table, Value}; /// A TOML [`Value`] that contains a collection of [`Key`]/[`Value`] pairs #[derive(Debug, Default, Clone)] @@ -276,7 +276,7 @@ impl InlineTable { } /// Gets the given key's corresponding entry in the Table for in-place manipulation. - pub fn entry(&'_ mut self, key: impl Into) -> InlineEntry<'_> { + pub fn entry(&'_ mut self, key: impl Into) -> InlineEntry<'_> { match self.items.entry(key.into().into()) { indexmap::map::Entry::Occupied(mut entry) => { // Ensure it is a `Value` to simplify `InlineOccupiedEntry`'s code. @@ -365,7 +365,7 @@ impl InlineTable { /// Returns a mutable reference to the corresponding value. pub fn get_or_insert>( &mut self, - key: impl Into, + key: impl Into, value: V, ) -> &mut Value { let key = key.into(); @@ -377,7 +377,7 @@ impl InlineTable { } /// Inserts a key-value pair into the map. - pub fn insert(&mut self, key: impl Into, value: Value) -> Option { + pub fn insert(&mut self, key: impl Into, value: Value) -> Option { use indexmap::map::MutableEntryKey; let key = Key::new(key); let value = Item::Value(value); @@ -472,7 +472,7 @@ impl, V: Into> FromIterator<(K, V)> for InlineTable { } impl IntoIterator for InlineTable { - type Item = (InternalString, Value); + type Item = (String, Value); type IntoIter = InlineTableIntoIter; fn into_iter(self) -> Self::IntoIter { @@ -509,7 +509,7 @@ fn decorate_inline_table(table: &mut InlineTable) { } /// An owned iterator type over an [`InlineTable`]'s [`Key`]/[`Value`] pairs -pub type InlineTableIntoIter = Box>; +pub type InlineTableIntoIter = Box>; /// An iterator type over [`InlineTable`]'s [`Key`]/[`Value`] pairs pub type InlineTableIter<'a> = Box + 'a>; /// A mutable iterator type over [`InlineTable`]'s [`Key`]/[`Value`] pairs @@ -531,7 +531,7 @@ impl TableLike for InlineTable { self.clear(); } fn entry<'a>(&'a mut self, key: &str) -> crate::Entry<'a> { - // Accept a `&str` rather than an owned type to keep `InternalString`, well, internal + // Accept a `&str` rather than an owned type to keep `String`, well, internal match self.items.entry(key.into()) { indexmap::map::Entry::Occupied(entry) => { crate::Entry::Occupied(crate::OccupiedEntry { entry }) diff --git a/crates/toml_edit/src/internal_string.rs b/crates/toml_edit/src/internal_string.rs deleted file mode 100644 index c8be22e4c..000000000 --- a/crates/toml_edit/src/internal_string.rs +++ /dev/null @@ -1,193 +0,0 @@ -use std::borrow::Borrow; -use std::str::FromStr; - -/// Opaque string storage internal to `toml_edit` -#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct InternalString(Inner); - -#[cfg(feature = "perf")] -type Inner = kstring::KString; -#[cfg(not(feature = "perf"))] -type Inner = String; - -impl InternalString { - /// Create an empty string - pub fn new() -> Self { - Self(Inner::new()) - } - - /// Access the underlying string - #[inline] - pub fn as_str(&self) -> &str { - self.0.as_str() - } -} - -impl std::fmt::Debug for InternalString { - #[inline] - fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - self.0.fmt(formatter) - } -} - -impl std::ops::Deref for InternalString { - type Target = str; - - #[inline] - fn deref(&self) -> &str { - self.as_str() - } -} - -impl Borrow for InternalString { - #[inline] - fn borrow(&self) -> &str { - self.as_str() - } -} - -impl AsRef for InternalString { - #[inline] - fn as_ref(&self) -> &str { - self.as_str() - } -} - -impl From<&str> for InternalString { - #[inline] - fn from(s: &str) -> Self { - #[cfg(feature = "perf")] - let inner = kstring::KString::from_ref(s); - #[cfg(not(feature = "perf"))] - let inner = String::from(s); - - Self(inner) - } -} - -impl From for InternalString { - #[inline] - fn from(s: String) -> Self { - #[allow(clippy::useless_conversion)] // handle any string type - Self(s.into()) - } -} - -impl From<&String> for InternalString { - #[inline] - fn from(s: &String) -> Self { - Self(s.into()) - } -} - -impl From<&Self> for InternalString { - #[inline] - fn from(s: &Self) -> Self { - s.clone() - } -} - -impl From> for InternalString { - #[inline] - fn from(s: std::borrow::Cow<'_, str>) -> Self { - match s { - std::borrow::Cow::Borrowed(s) => s.into(), - std::borrow::Cow::Owned(s) => s.into(), - } - } -} - -impl From> for InternalString { - #[inline] - fn from(s: Box) -> Self { - Self(s.into()) - } -} - -impl FromStr for InternalString { - type Err = core::convert::Infallible; - #[inline] - fn from_str(s: &str) -> Result { - Ok(Self::from(s)) - } -} - -impl std::fmt::Display for InternalString { - #[inline] - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.as_str().fmt(f) - } -} - -#[cfg(feature = "serde")] -impl serde::Serialize for InternalString { - #[inline] - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.serialize_str(self.as_str()) - } -} - -#[cfg(feature = "serde")] -impl<'de> serde::Deserialize<'de> for InternalString { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - deserializer.deserialize_string(StringVisitor) - } -} - -#[cfg(feature = "serde")] -struct StringVisitor; - -#[cfg(feature = "serde")] -impl serde::de::Visitor<'_> for StringVisitor { - type Value = InternalString; - - fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - formatter.write_str("a string") - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, - { - Ok(InternalString::from(v)) - } - - fn visit_string(self, v: String) -> Result - where - E: serde::de::Error, - { - Ok(InternalString::from(v)) - } - - fn visit_bytes(self, v: &[u8]) -> Result - where - E: serde::de::Error, - { - match std::str::from_utf8(v) { - Ok(s) => Ok(InternalString::from(s)), - Err(_) => Err(serde::de::Error::invalid_value( - serde::de::Unexpected::Bytes(v), - &self, - )), - } - } - - fn visit_byte_buf(self, v: Vec) -> Result - where - E: serde::de::Error, - { - match String::from_utf8(v) { - Ok(s) => Ok(InternalString::from(s)), - Err(e) => Err(serde::de::Error::invalid_value( - serde::de::Unexpected::Bytes(&e.into_bytes()), - &self, - )), - } - } -} diff --git a/crates/toml_edit/src/key.rs b/crates/toml_edit/src/key.rs index 0da6e97e9..ac18c860a 100644 --- a/crates/toml_edit/src/key.rs +++ b/crates/toml_edit/src/key.rs @@ -5,7 +5,6 @@ use std::str::FromStr; use toml_write::ToTomlKey as _; use crate::repr::{Decor, Repr}; -use crate::InternalString; /// For Key/[`Value`][crate::Value] pairs under a [`Table`][crate::Table] header or inside an /// [`InlineTable`][crate::InlineTable] @@ -32,7 +31,7 @@ use crate::InternalString; /// To parse a key use `FromStr` trait implementation: `"string".parse::()`. #[derive(Debug)] pub struct Key { - key: InternalString, + key: String, pub(crate) repr: Option, pub(crate) leaf_decor: Decor, pub(crate) dotted_decor: Decor, @@ -40,7 +39,7 @@ pub struct Key { impl Key { /// Create a new table key - pub fn new(key: impl Into) -> Self { + pub fn new(key: impl Into) -> Self { Self { key: key.into(), repr: None, @@ -292,14 +291,8 @@ impl From for Key { } } -impl From for Key { - fn from(s: InternalString) -> Self { - Self::new(s) - } -} - #[doc(hidden)] -impl From for InternalString { +impl From for String { fn from(key: Key) -> Self { key.key } diff --git a/crates/toml_edit/src/lib.rs b/crates/toml_edit/src/lib.rs index c9d777474..59cc14113 100644 --- a/crates/toml_edit/src/lib.rs +++ b/crates/toml_edit/src/lib.rs @@ -84,7 +84,6 @@ mod encode; mod error; mod index; mod inline_table; -mod internal_string; mod item; mod key; #[cfg(feature = "parse")] @@ -116,7 +115,6 @@ pub use crate::inline_table::{ InlineEntry, InlineOccupiedEntry, InlineTable, InlineTableIntoIter, InlineTableIter, InlineTableIterMut, InlineVacantEntry, }; -pub use crate::internal_string::InternalString; pub use crate::item::{array, table, value, Item}; pub use crate::key::{Key, KeyMut}; pub use crate::raw_string::RawString; diff --git a/crates/toml_edit/src/parser/key.rs b/crates/toml_edit/src/parser/key.rs index e987b7d7e..1c5f3cbf6 100644 --- a/crates/toml_edit/src/parser/key.rs +++ b/crates/toml_edit/src/parser/key.rs @@ -2,7 +2,6 @@ use crate::key::Key; use crate::parser::prelude::*; use crate::repr::Decor; use crate::repr::Repr; -use crate::InternalString; use crate::RawString; /// ```bnf @@ -133,9 +132,6 @@ impl State { let key_span = key.span(); let key_raw = RawString::with_span(key_span.start()..key_span.end()); - #[cfg(feature = "unsafe")] // SAFETY: lexing and parsing all with same source - let raw = unsafe { source.get_unchecked(key) }; - #[cfg(not(feature = "unsafe"))] let raw = source.get(key).unwrap(); let mut decoded = std::borrow::Cow::Borrowed(""); raw.decode_key(&mut decoded, errors); @@ -157,12 +153,9 @@ pub(crate) fn on_simple_key( event: &toml_parse::parser::Event, source: toml_parse::Source<'_>, errors: &mut dyn ErrorSink, -) -> (RawString, InternalString) { +) -> (RawString, String) { #[cfg(feature = "debug")] let _scope = TraceScope::new("key::on_simple_key"); - #[cfg(feature = "unsafe")] // SAFETY: lexing and parsing all with same source - let raw = unsafe { source.get_unchecked(event) }; - #[cfg(not(feature = "unsafe"))] let raw = source.get(event).unwrap(); let mut key = std::borrow::Cow::Borrowed(""); @@ -170,6 +163,6 @@ pub(crate) fn on_simple_key( let span = event.span(); let raw = RawString::with_span(span.start()..span.end()); - let key = InternalString::from(key); + let key = String::from(key); (raw, key) } diff --git a/crates/toml_edit/src/parser/value.rs b/crates/toml_edit/src/parser/value.rs index 0d40044d6..a742cf477 100644 --- a/crates/toml_edit/src/parser/value.rs +++ b/crates/toml_edit/src/parser/value.rs @@ -68,9 +68,6 @@ pub(crate) fn on_scalar( let value_span = event.span(); let value_raw = RawString::with_span(value_span.start()..value_span.end()); - #[cfg(feature = "unsafe")] // SAFETY: lexing and parsing all with same source - let raw = unsafe { source.get_unchecked(event) }; - #[cfg(not(feature = "unsafe"))] let raw = source.get(event).unwrap(); let mut decoded = std::borrow::Cow::Borrowed(""); let kind = raw.decode_scalar(&mut decoded, errors); diff --git a/crates/toml_edit/src/raw_string.rs b/crates/toml_edit/src/raw_string.rs index 40853a9c7..00c7d53d7 100644 --- a/crates/toml_edit/src/raw_string.rs +++ b/crates/toml_edit/src/raw_string.rs @@ -1,5 +1,3 @@ -use crate::InternalString; - /// Opaque string storage for raw TOML; internal to `toml_edit` #[derive(PartialEq, Eq, Clone, Hash)] pub struct RawString(RawStringInner); @@ -7,7 +5,7 @@ pub struct RawString(RawStringInner); #[derive(PartialEq, Eq, Clone, Hash)] enum RawStringInner { Empty, - Explicit(InternalString), + Explicit(String), Spanned(std::ops::Range), } @@ -131,7 +129,7 @@ impl From<&str> for RawString { if s.is_empty() { Self(RawStringInner::Empty) } else { - InternalString::from(s).into() + String::from(s).into() } } } @@ -142,7 +140,7 @@ impl From for RawString { if s.is_empty() { Self(RawStringInner::Empty) } else { - InternalString::from(s).into() + Self(RawStringInner::Explicit(s)) } } } @@ -153,25 +151,7 @@ impl From<&String> for RawString { if s.is_empty() { Self(RawStringInner::Empty) } else { - InternalString::from(s).into() - } - } -} - -impl From for RawString { - #[inline] - fn from(inner: InternalString) -> Self { - Self(RawStringInner::Explicit(inner)) - } -} - -impl From<&InternalString> for RawString { - #[inline] - fn from(s: &InternalString) -> Self { - if s.is_empty() { - Self(RawStringInner::Empty) - } else { - InternalString::from(s).into() + String::from(s).into() } } } @@ -182,7 +162,7 @@ impl From> for RawString { if s.is_empty() { Self(RawStringInner::Empty) } else { - InternalString::from(s).into() + String::from(s).into() } } } diff --git a/crates/toml_edit/src/table.rs b/crates/toml_edit/src/table.rs index 0ad02da36..b4391f046 100644 --- a/crates/toml_edit/src/table.rs +++ b/crates/toml_edit/src/table.rs @@ -5,7 +5,7 @@ use indexmap::map::IndexMap; use crate::key::Key; use crate::repr::Decor; use crate::value::DEFAULT_VALUE_DECOR; -use crate::{InlineTable, InternalString, Item, KeyMut, Value}; +use crate::{InlineTable, Item, KeyMut, Value}; /// A TOML table, a top-level collection of key/[`Value`] pairs under a header and logical /// sub-tables @@ -328,7 +328,7 @@ impl Table { /// Gets the given key's corresponding entry in the Table for in-place manipulation. pub fn entry<'a>(&'a mut self, key: &str) -> Entry<'a> { - // Accept a `&str` rather than an owned type to keep `InternalString`, well, internal + // Accept a `&str` rather than an owned type to keep `String`, well, internal match self.items.entry(key.into()) { indexmap::map::Entry::Occupied(entry) => Entry::Occupied(OccupiedEntry { entry }), indexmap::map::Entry::Vacant(entry) => Entry::Vacant(VacantEntry { entry }), @@ -511,7 +511,7 @@ impl, V: Into> FromIterator<(K, V)> for Table { } impl IntoIterator for Table { - type Item = (InternalString, Item); + type Item = (String, Item); type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { @@ -551,7 +551,7 @@ pub(crate) const DEFAULT_TABLE_DECOR: (&str, &str) = ("\n", ""); pub(crate) const DEFAULT_KEY_PATH_DECOR: (&str, &str) = ("", ""); /// An owned iterator type over [`Table`]'s [`Key`]/[`Item`] pairs -pub type IntoIter = Box>; +pub type IntoIter = Box>; /// An iterator type over [`Table`]'s [`Key`]/[`Item`] pairs pub type Iter<'a> = Box + 'a>; /// A mutable iterator type over [`Table`]'s [`Key`]/[`Item`] pairs diff --git a/crates/toml_edit/src/value.rs b/crates/toml_edit/src/value.rs index ed53b0578..f39f16556 100644 --- a/crates/toml_edit/src/value.rs +++ b/crates/toml_edit/src/value.rs @@ -5,7 +5,7 @@ use toml_datetime::{Date, Datetime, Time}; use crate::key::Key; use crate::repr::{Decor, Formatted}; -use crate::{Array, InlineTable, InternalString, RawString}; +use crate::{Array, InlineTable, RawString}; /// For [`Key`]/Value pairs under a [`Table`][crate::Table] header or inside another /// Value @@ -279,18 +279,6 @@ impl From for Value { } } -impl<'b> From<&'b InternalString> for Value { - fn from(s: &'b InternalString) -> Self { - s.as_str().into() - } -} - -impl From for Value { - fn from(s: InternalString) -> Self { - s.as_str().into() - } -} - impl From for Value { fn from(i: i64) -> Self { Self::Integer(Formatted::new(i))