Skip to content

Commit 2f0ee3e

Browse files
committed
Replace Ident2 with 2-word Cow from beef crate
1 parent 99fe5c2 commit 2f0ee3e

5 files changed

Lines changed: 28 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/dreamchecker/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,12 +2267,12 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
22672267
},
22682268
Term::String(text) => Analysis::from_value(
22692269
self.objtree,
2270-
Constant::String(text.as_str().into()),
2270+
Constant::String(text.clone().into()),
22712271
type_hint,
22722272
),
22732273
Term::Resource(text) => Analysis::from_value(
22742274
self.objtree,
2275-
Constant::Resource(text.as_str().into()),
2275+
Constant::Resource(text.clone().into()),
22762276
type_hint,
22772277
),
22782278
Term::As(_) => assumption_set![Assumption::IsNum(true)].into(),
@@ -2960,9 +2960,7 @@ impl<'o, 's> AnalyzeProc<'o, 's> {
29602960
}
29612961
typeref.get().pretty_path()
29622962
},
2963-
StaticType::List { list, .. } => {
2964-
"list"
2965-
},
2963+
StaticType::List { list, .. } => "list",
29662964
StaticType::Proc => return Analysis::empty(),
29672965
};
29682966
error(

crates/dreammaker/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ indexmap = "2.6.0"
2121
derivative = "2.2.0"
2222
get-size = "0.1.4"
2323
get-size-derive = "0.1.3"
24+
beef = "0.5.2"
2425

2526
[dev-dependencies]
2627
walkdir = "2.5.0"

crates/dreammaker/src/ast.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::fmt;
55
use std::iter::FromIterator;
66

7+
use beef::lean::Cow;
78
use get_size::GetSize;
89
use get_size_derive::GetSize;
910
use phf::phf_map;
@@ -702,13 +703,17 @@ pub type Ident = String;
702703
// but could be replaced by interning later.
703704
#[derive(Clone, Eq, PartialEq)]
704705
pub struct Ident2 {
705-
inner: Box<str>,
706+
inner: Cow<'static, str>,
706707
}
707708

708709
impl Ident2 {
709710
pub fn as_str(&self) -> &str {
710711
&self.inner
711712
}
713+
714+
pub fn into_owned(self) -> String {
715+
self.inner.into_owned()
716+
}
712717
}
713718

714719
impl PartialEq<str> for Ident2 {
@@ -717,8 +722,8 @@ impl PartialEq<str> for Ident2 {
717722
}
718723
}
719724

720-
impl<'a> From<&'a str> for Ident2 {
721-
fn from(v: &'a str) -> Self {
725+
impl From<&'static str> for Ident2 {
726+
fn from(v: &'static str) -> Self {
722727
Ident2 { inner: v.into() }
723728
}
724729
}
@@ -729,12 +734,6 @@ impl From<String> for Ident2 {
729734
}
730735
}
731736

732-
impl From<Ident2> for String {
733-
fn from(v: Ident2) -> Self {
734-
v.inner.into()
735-
}
736-
}
737-
738737
impl std::ops::Deref for Ident2 {
739738
type Target = str;
740739
fn deref(&self) -> &str {
@@ -756,7 +755,11 @@ impl fmt::Debug for Ident2 {
756755

757756
impl GetSize for Ident2 {
758757
fn get_heap_size(&self) -> usize {
759-
self.inner.len()
758+
if self.inner.is_owned() {
759+
self.inner.len()
760+
} else {
761+
0
762+
}
760763
}
761764
}
762765

@@ -1541,6 +1544,7 @@ pub static VALID_FILTER_FLAGS: phf::Map<&'static str, (&str, bool, bool, &[&str]
15411544

15421545
// ----------------------------------------------------------------------------
15431546
// Guard against sizeof regression.
1547+
const _: [(); 0 - !(std::mem::size_of::<Ident2>() <= 16) as usize] = [];
15441548
const _: [(); 0 - !(std::mem::size_of::<Statement>() <= 56) as usize] = [];
15451549
const _: [(); 0 - !(std::mem::size_of::<Expression>() <= 32) as usize] = [];
15461550
const _: [(); 0 - !(std::mem::size_of::<Term>() <= 40) as usize] = [];

crates/dreammaker/src/constants.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ impl<'a> ConstantFolder<'a> {
820820

821821
match (op, lhs, rhs) {
822822
(BinaryOp::Add, String(lhs), String(rhs)) => {
823-
Ok(String((std::string::String::from(lhs) + &rhs).into()))
823+
Ok(String((lhs.into_owned() + &rhs).into()))
824824
},
825825
(BinaryOp::Eq, lhs, rhs) => Ok(Constant::from(lhs == rhs)),
826826
(BinaryOp::NotEq, lhs, rhs) => Ok(Constant::from(lhs != rhs)),
@@ -888,7 +888,7 @@ impl<'a> ConstantFolder<'a> {
888888
)));
889889
}
890890
match args[0].nameof() {
891-
Some(name) => Constant::string(name),
891+
Some(name) => Constant::string(name.to_owned()),
892892
None => {
893893
return Err(self.error(
894894
"malformed nameof() call, expression appears to have no name",
@@ -1062,7 +1062,7 @@ impl<'a> ConstantFolder<'a> {
10621062
let mut vars = IndexMap::with_hasher(RandomState::default());
10631063
for (k, v) in input {
10641064
// TODO: find a type annotation by looking up 'k' on the prefab's type
1065-
vars.insert(String::from(k), self.expr(v, None)?);
1065+
vars.insert(k.into_owned(), self.expr(v, None)?);
10661066
}
10671067
Ok(vars)
10681068
}

0 commit comments

Comments
 (0)