Skip to content

Commit 2aed203

Browse files
committed
use Arc<str> instead of String in pathes
1 parent 6464100 commit 2aed203

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

svd-parser/src/expand.rs

+34-36
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Provides [expand] method to convert arrays, clusters and derived items in regular instances
22
33
use anyhow::{anyhow, Result};
4-
use std::collections::HashMap;
5-
use std::fmt;
6-
use std::mem::take;
4+
use std::{collections::HashMap, fmt, mem::take, rc::Rc};
75
use svd_rs::{
86
array::names, cluster, field, peripheral, register, Cluster, ClusterInfo, DeriveFrom, Device,
97
EnumeratedValues, Field, Peripheral, Register, RegisterCluster, RegisterProperties,
@@ -12,23 +10,23 @@ use svd_rs::{
1210
/// Path to `peripheral` or `cluster` element
1311
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
1412
pub struct BlockPath {
15-
pub peripheral: String,
16-
pub path: Vec<String>,
13+
pub peripheral: Rc<str>,
14+
pub path: Vec<Rc<str>>,
1715
}
1816

1917
impl BlockPath {
20-
pub fn new(p: impl Into<String>) -> Self {
18+
pub fn new(p: impl Into<Rc<str>>) -> Self {
2119
Self {
2220
peripheral: p.into(),
2321
path: Vec::new(),
2422
}
2523
}
26-
pub fn new_cluster(&self, name: impl Into<String>) -> Self {
24+
pub fn new_cluster(&self, name: impl Into<Rc<str>>) -> Self {
2725
let mut child = self.clone();
2826
child.path.push(name.into());
2927
child
3028
}
31-
pub fn new_register(&self, name: impl Into<String>) -> RegisterPath {
29+
pub fn new_register(&self, name: impl Into<Rc<str>>) -> RegisterPath {
3230
RegisterPath::new(self.clone(), name)
3331
}
3432
pub fn parse_str(s: &str) -> (Option<Self>, &str) {
@@ -46,7 +44,7 @@ impl BlockPath {
4644
};
4745
(block, name)
4846
}
49-
pub fn name(&self) -> &String {
47+
pub fn name(&self) -> &str {
5048
self.path.last().unwrap()
5149
}
5250
pub fn parent(&self) -> Option<Self> {
@@ -63,11 +61,11 @@ impl PartialEq<str> for BlockPath {
6361
}
6462
let mut parts = other.split('.');
6563
if let Some(part1) = parts.next() {
66-
if self.peripheral != part1 {
64+
if self.peripheral.as_ref() != part1 {
6765
return false;
6866
}
6967
for p in parts.zip(self.path.iter()) {
70-
if p.0 != p.1 {
68+
if p.0 != p.1.as_ref() {
7169
return false;
7270
}
7371
}
@@ -93,17 +91,17 @@ impl fmt::Display for BlockPath {
9391
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
9492
pub struct RegisterPath {
9593
pub block: BlockPath,
96-
pub name: String,
94+
pub name: Rc<str>,
9795
}
9896

9997
impl RegisterPath {
100-
pub fn new(block: BlockPath, name: impl Into<String>) -> Self {
98+
pub fn new(block: BlockPath, name: impl Into<Rc<str>>) -> Self {
10199
Self {
102100
block,
103101
name: name.into(),
104102
}
105103
}
106-
pub fn new_field(&self, name: impl Into<String>) -> FieldPath {
104+
pub fn new_field(&self, name: impl Into<Rc<str>>) -> FieldPath {
107105
FieldPath::new(self.clone(), name)
108106
}
109107
pub fn parse_str(s: &str) -> (Option<BlockPath>, &str) {
@@ -112,15 +110,15 @@ impl RegisterPath {
112110
pub fn parse_vec(v: Vec<&str>) -> (Option<BlockPath>, &str) {
113111
BlockPath::parse_vec(v)
114112
}
115-
pub fn peripheral(&self) -> &String {
113+
pub fn peripheral(&self) -> &str {
116114
&self.block.peripheral
117115
}
118116
}
119117

120118
impl PartialEq<str> for RegisterPath {
121119
fn eq(&self, other: &str) -> bool {
122120
if let Some((block, reg)) = other.rsplit_once('.') {
123-
self.name == reg && &self.block == block
121+
self.name.as_ref() == reg && &self.block == block
124122
} else {
125123
false
126124
}
@@ -140,17 +138,17 @@ impl fmt::Display for RegisterPath {
140138
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
141139
pub struct FieldPath {
142140
pub register: RegisterPath,
143-
pub name: String,
141+
pub name: Rc<str>,
144142
}
145143

146144
impl FieldPath {
147-
pub fn new(register: RegisterPath, name: impl Into<String>) -> Self {
145+
pub fn new(register: RegisterPath, name: impl Into<Rc<str>>) -> Self {
148146
Self {
149147
register,
150148
name: name.into(),
151149
}
152150
}
153-
pub fn new_enum(&self, name: impl Into<String>) -> EnumPath {
151+
pub fn new_enum(&self, name: impl Into<Rc<str>>) -> EnumPath {
154152
EnumPath::new(self.clone(), name)
155153
}
156154
pub fn parse_str(s: &str) -> (Option<RegisterPath>, &str) {
@@ -172,15 +170,15 @@ impl FieldPath {
172170
pub fn register(&self) -> &RegisterPath {
173171
&self.register
174172
}
175-
pub fn peripheral(&self) -> &String {
173+
pub fn peripheral(&self) -> &str {
176174
self.register.peripheral()
177175
}
178176
}
179177

180178
impl PartialEq<str> for FieldPath {
181179
fn eq(&self, other: &str) -> bool {
182180
if let Some((reg, field)) = other.rsplit_once('.') {
183-
self.name == field && &self.register == reg
181+
self.name.as_ref() == field && &self.register == reg
184182
} else {
185183
false
186184
}
@@ -200,11 +198,11 @@ impl fmt::Display for FieldPath {
200198
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
201199
pub struct EnumPath {
202200
pub field: FieldPath,
203-
pub name: String,
201+
pub name: Rc<str>,
204202
}
205203

206204
impl EnumPath {
207-
pub fn new(field: FieldPath, name: impl Into<String>) -> Self {
205+
pub fn new(field: FieldPath, name: impl Into<Rc<str>>) -> Self {
208206
Self {
209207
field,
210208
name: name.into(),
@@ -216,15 +214,15 @@ impl EnumPath {
216214
pub fn register(&self) -> &RegisterPath {
217215
&self.field.register
218216
}
219-
pub fn peripheral(&self) -> &String {
217+
pub fn peripheral(&self) -> &str {
220218
self.field.peripheral()
221219
}
222220
}
223221

224222
impl PartialEq<str> for EnumPath {
225223
fn eq(&self, other: &str) -> bool {
226224
if let Some((field, evs)) = other.rsplit_once('.') {
227-
self.name == evs && &self.field == field
225+
self.name.as_ref() == evs && &self.field == field
228226
} else {
229227
false
230228
}
@@ -263,7 +261,7 @@ impl<'a> Index<'a> {
263261
self.peripherals.insert(path, p);
264262
}
265263
}
266-
let path = BlockPath::new(&p.name);
264+
let path = BlockPath::new(p.name.as_ref());
267265
for r in p.registers() {
268266
self.add_register(&path, r);
269267
}
@@ -286,7 +284,7 @@ impl<'a> Index<'a> {
286284
self.clusters.insert(cpath, c);
287285
}
288286
}
289-
let cpath = path.new_cluster(&c.name);
287+
let cpath = path.new_cluster(c.name.as_ref());
290288
for r in c.registers() {
291289
self.add_register(&cpath, r);
292290
}
@@ -305,7 +303,7 @@ impl<'a> Index<'a> {
305303
self.registers.insert(rpath, r);
306304
}
307305
}
308-
let rpath = path.new_register(&r.name);
306+
let rpath = path.new_register(r.name.as_ref());
309307
for f in r.fields() {
310308
self.add_field(&rpath, f);
311309
}
@@ -317,16 +315,16 @@ impl<'a> Index<'a> {
317315
let fpath = path.new_field(name);
318316
for evs in &f.enumerated_values {
319317
if let Some(name) = evs.name.as_ref() {
320-
self.evs.insert(fpath.new_enum(name), evs);
318+
self.evs.insert(fpath.new_enum(name.as_ref()), evs);
321319
}
322320
}
323321
self.fields.insert(fpath, f);
324322
}
325323
}
326-
let fpath = path.new_field(&f.name);
324+
let fpath = path.new_field(f.name.as_ref());
327325
for evs in &f.enumerated_values {
328326
if let Some(name) = evs.name.as_ref() {
329-
self.evs.insert(fpath.new_enum(name), evs);
327+
self.evs.insert(fpath.new_enum(name.as_ref()), evs);
330328
}
331329
}
332330
self.fields.insert(fpath, f);
@@ -365,7 +363,7 @@ fn expand_cluster_array(
365363
if let Some(dpath) = dpath {
366364
cpath = derive_cluster(&mut c, &dpath, path, index)?;
367365
}
368-
let cpath = cpath.unwrap_or_else(|| path.new_cluster(&c.name));
366+
let cpath = cpath.unwrap_or_else(|| path.new_cluster(c.name.as_ref()));
369367

370368
for rc in take(&mut c.children) {
371369
expand_register_cluster(&mut c.children, rc, &cpath, index)?;
@@ -499,7 +497,7 @@ fn expand_register_array(
499497
if let Some(dpath) = dpath {
500498
rpath = derive_register(&mut r, &dpath, path, index)?;
501499
}
502-
let rpath = rpath.unwrap_or_else(|| path.new_register(&r.name));
500+
let rpath = rpath.unwrap_or_else(|| path.new_register(r.name.as_ref()));
503501

504502
if let Some(field) = r.fields.as_mut() {
505503
for f in take(field) {
@@ -529,7 +527,7 @@ fn expand_field(
529527
if let Some(dpath) = dpath {
530528
fpath = derive_field(&mut f, &dpath, rpath, index)?;
531529
}
532-
let fpath = fpath.unwrap_or_else(|| rpath.new_field(&f.name));
530+
let fpath = fpath.unwrap_or_else(|| rpath.new_field(f.name.as_ref()));
533531

534532
for ev in &mut f.enumerated_values {
535533
let dpath = ev.derived_from.take();
@@ -564,7 +562,7 @@ pub fn derive_enumerated_values(
564562
if let Some(r) = index.registers.get(rdpath) {
565563
let mut found = None;
566564
for f in r.fields() {
567-
let epath = EnumPath::new(rdpath.new_field(&f.name), dname);
565+
let epath = EnumPath::new(rdpath.new_field(f.name.as_ref()), dname);
568566
if let Some(d) = index.evs.get(&epath) {
569567
found = Some((d, epath));
570568
break;
@@ -645,7 +643,7 @@ pub fn expand(indevice: &Device) -> Result<Device> {
645643
if let Some(dpath) = dpath {
646644
path = derive_peripheral(&mut p, &dpath, &index)?;
647645
}
648-
let path = path.unwrap_or_else(|| BlockPath::new(&p.name));
646+
let path = path.unwrap_or_else(|| BlockPath::new(p.name.as_ref()));
649647
if let Some(regs) = p.registers.as_mut() {
650648
for rc in take(regs) {
651649
expand_register_cluster(regs, rc, &path, &index)?;

0 commit comments

Comments
 (0)