Skip to content

Commit a0319e6

Browse files
committed
impl DeriveFrom for Register/Cluster/Field
1 parent c61f09f commit a0319e6

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

svd-rs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## Unreleased
9+
10+
- Implement `DeriveFrom` for `Cluster`, `Register` and `Field`
11+
812
## [v0.11.1] - 2021-10-02
913

1014
- Reexport builders

svd-rs/src/derive_from.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Implementations of DeriveFrom, setting non-explicit fields.
22
use crate::{
3-
ClusterInfo, EnumeratedValues, FieldInfo, Peripheral, RegisterInfo, RegisterProperties,
3+
Cluster, ClusterInfo, EnumeratedValues, Field, FieldInfo, Peripheral, Register, RegisterInfo,
4+
RegisterProperties,
45
};
56

67
/// Fill empty fields of structure with values of other structure
@@ -53,7 +54,7 @@ impl DeriveFrom for Peripheral {
5354
}
5455

5556
impl DeriveFrom for RegisterInfo {
56-
fn derive_from(&self, other: &RegisterInfo) -> RegisterInfo {
57+
fn derive_from(&self, other: &Self) -> Self {
5758
let mut derived = self.clone();
5859
derived.description = derived.description.or_else(|| other.description.clone());
5960
derived.properties = derived.properties.derive_from(&other.properties);
@@ -92,3 +93,54 @@ impl DeriveFrom for FieldInfo {
9293
derived
9394
}
9495
}
96+
97+
impl DeriveFrom for Cluster {
98+
fn derive_from(&self, other: &Self) -> Self {
99+
match (self, other) {
100+
(Self::Single(info), Self::Single(other_info)) => {
101+
Self::Single(info.derive_from(other_info))
102+
}
103+
(Self::Single(info), Self::Array(other_info, other_dim)) => {
104+
Self::Array(info.derive_from(other_info), other_dim.clone())
105+
}
106+
(Self::Array(info, dim), Self::Single(other_info))
107+
| (Self::Array(info, dim), Self::Array(other_info, _)) => {
108+
Self::Array(info.derive_from(other_info), dim.clone())
109+
}
110+
}
111+
}
112+
}
113+
114+
impl DeriveFrom for Register {
115+
fn derive_from(&self, other: &Self) -> Self {
116+
match (self, other) {
117+
(Self::Single(info), Self::Single(other_info)) => {
118+
Self::Single(info.derive_from(other_info))
119+
}
120+
(Self::Single(info), Self::Array(other_info, other_dim)) => {
121+
Self::Array(info.derive_from(other_info), other_dim.clone())
122+
}
123+
(Self::Array(info, dim), Self::Single(other_info))
124+
| (Self::Array(info, dim), Self::Array(other_info, _)) => {
125+
Self::Array(info.derive_from(other_info), dim.clone())
126+
}
127+
}
128+
}
129+
}
130+
131+
impl DeriveFrom for Field {
132+
fn derive_from(&self, other: &Self) -> Self {
133+
match (self, other) {
134+
(Self::Single(info), Self::Single(other_info)) => {
135+
Self::Single(info.derive_from(other_info))
136+
}
137+
(Self::Single(info), Self::Array(other_info, other_dim)) => {
138+
Self::Array(info.derive_from(other_info), other_dim.clone())
139+
}
140+
(Self::Array(info, dim), Self::Single(other_info))
141+
| (Self::Array(info, dim), Self::Array(other_info, _)) => {
142+
Self::Array(info.derive_from(other_info), dim.clone())
143+
}
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)