Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small improvements to interning #5153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/cargo/core/interning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,37 @@ impl InternedString {
cache.insert(s);
InternedString { ptr: s.as_ptr(), len: s.len() }
}
pub fn to_inner(&self) -> &'static str {
unsafe {
let slice = slice::from_raw_parts(self.ptr, self.len);
&str::from_utf8_unchecked(slice)
}
}
}

impl Deref for InternedString {
type Target = str;

fn deref(&self) -> &'static str {
unsafe {
let slice = slice::from_raw_parts(self.ptr, self.len);
&str::from_utf8_unchecked(slice)
}
self.to_inner()
}
}

impl fmt::Debug for InternedString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str: &str = &*self;
write!(f, "InternedString {{ {} }}", str)
write!(f, "InternedString {{ {} }}", self.to_inner())
}
}

impl fmt::Display for InternedString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_inner())
}
}

impl Ord for InternedString {
fn cmp(&self, other: &InternedString) -> Ordering {
let str: &str = &*self;
str.cmp(&*other)
self.to_inner().cmp(&*other)
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::ser;

use util::{CargoResult, ToSemver};
use core::source::SourceId;
use core::interning::InternedString;

/// Identifier for a specific version of a package in a specific source.
#[derive(Clone)]
Expand All @@ -20,7 +21,7 @@ pub struct PackageId {

#[derive(PartialEq, PartialOrd, Eq, Ord)]
struct PackageIdInner {
name: String,
name: InternedString,
version: semver::Version,
source_id: SourceId,
}
Expand Down Expand Up @@ -63,7 +64,7 @@ impl<'de> de::Deserialize<'de> for PackageId {

Ok(PackageId {
inner: Arc::new(PackageIdInner {
name: name.to_string(),
name: InternedString::new(name),
version,
source_id,
}),
Expand Down Expand Up @@ -102,21 +103,21 @@ impl PackageId {
let v = version.to_semver()?;
Ok(PackageId {
inner: Arc::new(PackageIdInner {
name: name.to_string(),
name: InternedString::new(name),
version: v,
source_id: sid.clone(),
}),
})
}

pub fn name(&self) -> &str { &self.inner.name }
pub fn name(&self) -> &str { self.inner.name.to_inner() }
pub fn version(&self) -> &semver::Version { &self.inner.version }
pub fn source_id(&self) -> &SourceId { &self.inner.source_id }

pub fn with_precise(&self, precise: Option<String>) -> PackageId {
PackageId {
inner: Arc::new(PackageIdInner {
name: self.inner.name.to_string(),
name: self.inner.name,
version: self.inner.version.clone(),
source_id: self.inner.source_id.with_precise(precise),
}),
Expand All @@ -126,7 +127,7 @@ impl PackageId {
pub fn with_source_id(&self, source: &SourceId) -> PackageId {
PackageId {
inner: Arc::new(PackageIdInner {
name: self.inner.name.to_string(),
name: self.inner.name,
version: self.inner.version.clone(),
source_id: source.clone(),
}),
Expand Down