-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use arcstr for package, extra, and group names
- Loading branch information
1 parent
503f9a9
commit b8874a2
Showing
9 changed files
with
148 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::cmp::PartialEq; | ||
use std::ops::Deref; | ||
// | ||
// use rkyv::rancor::{Fallible, Source}; | ||
// use rkyv::ser::{Allocator, Writer}; | ||
// use rkyv::string::{ArchivedString, StringResolver}; | ||
// use rkyv::{Archive, Place}; | ||
|
||
/// An optimized small string type for short identifiers, like package names. | ||
/// | ||
/// Represented as an [`arcstr::ArcStr`] internally. | ||
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub(crate) struct SmallString(arcstr::ArcStr); | ||
|
||
impl From<&str> for SmallString { | ||
fn from(s: &str) -> Self { | ||
Self(s.into()) | ||
} | ||
} | ||
|
||
impl From<String> for SmallString { | ||
fn from(s: String) -> Self { | ||
Self(s.into()) | ||
} | ||
} | ||
|
||
impl AsRef<str> for SmallString { | ||
fn as_ref(&self) -> &str { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Deref for SmallString { | ||
type Target = str; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
/// A [`serde::Serialize`] implementation for [`SmallString`]. | ||
impl serde::Serialize for SmallString { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
self.0.serialize(serializer) | ||
} | ||
} | ||
|
||
/// An [`rkyv`] implementation for [`SmallString`]. | ||
impl rkyv::Archive for SmallString { | ||
type Archived = rkyv::string::ArchivedString; | ||
type Resolver = rkyv::string::StringResolver; | ||
|
||
#[inline] | ||
fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place<Self::Archived>) { | ||
rkyv::string::ArchivedString::resolve_from_str(&self.0, resolver, out); | ||
} | ||
} | ||
|
||
impl<S> rkyv::Serialize<S> for SmallString | ||
where | ||
S: rkyv::rancor::Fallible + rkyv::ser::Allocator + rkyv::ser::Writer + ?Sized, | ||
S::Error: rkyv::rancor::Source, | ||
{ | ||
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> { | ||
rkyv::string::ArchivedString::serialize_from_str(&self.0, serializer) | ||
} | ||
} | ||
|
||
impl<D: rkyv::rancor::Fallible + ?Sized> rkyv::Deserialize<SmallString, D> | ||
for rkyv::string::ArchivedString | ||
{ | ||
fn deserialize(&self, _deserializer: &mut D) -> Result<SmallString, D::Error> { | ||
Ok(SmallString::from(self.as_str())) | ||
} | ||
} | ||
|
||
impl PartialEq<SmallString> for rkyv::string::ArchivedString { | ||
fn eq(&self, other: &SmallString) -> bool { | ||
**other == **self | ||
} | ||
} | ||
|
||
impl PartialOrd<SmallString> for rkyv::string::ArchivedString { | ||
fn partial_cmp(&self, other: &SmallString) -> Option<::core::cmp::Ordering> { | ||
Some(self.as_str().cmp(other)) | ||
} | ||
} | ||
|
||
/// An [`schemars::JsonSchema`] implementation for [`SmallString`]. | ||
#[cfg(feature = "schemars")] | ||
impl schemars::JsonSchema for SmallString { | ||
fn schema_name() -> String { | ||
String::schema_name() | ||
} | ||
|
||
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { | ||
String::json_schema(_gen) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters