Skip to content

Commit

Permalink
use Arc<str> instead of String in pathes
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Aug 22, 2024
1 parent c5be07f commit dcca7ef
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions svd-parser/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Provides [expand] method to convert arrays, clusters and derived items in regular instances

use anyhow::{anyhow, Result};
use std::{collections::HashMap, fmt, mem::take, ops::Deref};
use std::{collections::HashMap, fmt, mem::take, ops::Deref, rc::Rc};
use svd_rs::{
array::names, cluster, field, peripheral, register, Cluster, ClusterInfo, DeriveFrom, Device,
EnumeratedValues, Field, Peripheral, Register, RegisterCluster, RegisterProperties,
Expand All @@ -10,23 +10,23 @@ use svd_rs::{
/// Path to `peripheral` or `cluster` element
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub struct BlockPath {
pub peripheral: String,
pub path: Vec<String>,
pub peripheral: Rc<str>,
pub path: Vec<Rc<str>>,
}

impl BlockPath {
pub fn new(p: impl Into<String>) -> Self {
pub fn new(p: impl Into<Rc<str>>) -> Self {
Self {
peripheral: p.into(),
path: Vec::new(),
}
}
pub fn new_cluster(&self, name: impl Into<String>) -> Self {
pub fn new_cluster(&self, name: impl Into<Rc<str>>) -> Self {
let mut child = self.clone();
child.path.push(name.into());
child
}
pub fn new_register(&self, name: impl Into<String>) -> RegisterPath {
pub fn new_register(&self, name: impl Into<Rc<str>>) -> RegisterPath {
RegisterPath::new(self.clone(), name)
}
pub fn parse_str(s: &str) -> (Option<Self>, &str) {
Expand All @@ -44,7 +44,7 @@ impl BlockPath {
};
(block, name)
}
pub fn name(&self) -> &String {
pub fn name(&self) -> &str {
self.path.last().unwrap()
}
pub fn parent(&self) -> Option<Self> {
Expand Down Expand Up @@ -91,17 +91,17 @@ impl fmt::Display for BlockPath {
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub struct RegisterPath {
pub block: BlockPath,
pub name: String,
pub name: Rc<str>,
}

impl RegisterPath {
pub fn new(block: BlockPath, name: impl Into<String>) -> Self {
pub fn new(block: BlockPath, name: impl Into<Rc<str>>) -> Self {
Self {
block,
name: name.into(),
}
}
pub fn new_field(&self, name: impl Into<String>) -> FieldPath {
pub fn new_field(&self, name: impl Into<Rc<str>>) -> FieldPath {
FieldPath::new(self.clone(), name)
}
pub fn parse_str(s: &str) -> (Option<BlockPath>, &str) {
Expand All @@ -110,7 +110,7 @@ impl RegisterPath {
pub fn parse_vec(v: Vec<&str>) -> (Option<BlockPath>, &str) {
BlockPath::parse_vec(v)
}
pub fn peripheral(&self) -> &String {
pub fn peripheral(&self) -> &str {
&self.block.peripheral
}
}
Expand Down Expand Up @@ -138,17 +138,17 @@ impl fmt::Display for RegisterPath {
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub struct FieldPath {
pub register: RegisterPath,
pub name: String,
pub name: Rc<str>,
}

impl FieldPath {
pub fn new(register: RegisterPath, name: impl Into<String>) -> Self {
pub fn new(register: RegisterPath, name: impl Into<Rc<str>>) -> Self {
Self {
register,
name: name.into(),
}
}
pub fn new_enum(&self, name: impl Into<String>) -> EnumPath {
pub fn new_enum(&self, name: impl Into<Rc<str>>) -> EnumPath {
EnumPath::new(self.clone(), name)
}
pub fn parse_str(s: &str) -> (Option<RegisterPath>, &str) {
Expand All @@ -170,7 +170,7 @@ impl FieldPath {
pub fn register(&self) -> &RegisterPath {
&self.register
}
pub fn peripheral(&self) -> &String {
pub fn peripheral(&self) -> &str {
self.register.peripheral()
}
}
Expand Down Expand Up @@ -198,11 +198,11 @@ impl fmt::Display for FieldPath {
#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub struct EnumPath {
pub field: FieldPath,
pub name: String,
pub name: Rc<str>,
}

impl EnumPath {
pub fn new(field: FieldPath, name: impl Into<String>) -> Self {
pub fn new(field: FieldPath, name: impl Into<Rc<str>>) -> Self {
Self {
field,
name: name.into(),
Expand All @@ -214,7 +214,7 @@ impl EnumPath {
pub fn register(&self) -> &RegisterPath {
&self.field.register
}
pub fn peripheral(&self) -> &String {
pub fn peripheral(&self) -> &str {
self.field.peripheral()
}
}
Expand Down

0 comments on commit dcca7ef

Please sign in to comment.