Skip to content

Commit e50bca5

Browse files
committed
add FdtMut::reborrow()
1 parent 42c491a commit e50bca5

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

src/fdt_mut.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a> FdtMut<'a> {
136136
FdtNodeMut {
137137
offset: root.offset,
138138
parent_address_space: root.parent_address_space,
139-
data: self.data,
139+
data: self.reborrow(),
140140
}
141141
}
142142

@@ -163,9 +163,15 @@ impl<'a> FdtMut<'a> {
163163
Some(FdtNodeMut {
164164
offset: node.offset,
165165
parent_address_space: node.parent_address_space,
166-
data: self.data,
166+
data: self.reborrow(),
167167
})
168168
}
169+
170+
fn reborrow(&mut self) -> FdtMut<'_> {
171+
FdtMut {
172+
data: self.data,
173+
}
174+
}
169175
}
170176

171177
impl Debug for FdtMut<'_> {

src/fdt_mut/node.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ use core::fmt::{Display, Formatter};
1212
use crate::fdt::node::InnerChildIter;
1313
use crate::fdt::node::private::FdtChildIter;
1414
use crate::fdt::property::{FdtPropIter, InnerPropIter};
15-
use crate::fdt::{Fdt, FdtNode};
16-
use crate::fdt_mut::FdtPropertyMut;
15+
use crate::fdt::FdtNode;
16+
use crate::fdt_mut::{FdtMut, FdtPropertyMut};
1717
use crate::fdt_mut::property::FdtPropMutIter;
1818
use crate::standard::{AddressSpaceProperties, NodeStandard};
1919
use crate::{Node, Property};
2020

2121
/// A mutable device tree node.
2222
#[derive(Debug)]
2323
pub struct FdtNodeMut<'a> {
24-
pub(crate) data: &'a mut [u8],
24+
pub(crate) data: FdtMut<'a>,
2525
pub(crate) offset: usize,
2626
pub(crate) parent_address_space: AddressSpaceProperties,
2727
}
@@ -37,7 +37,7 @@ impl FdtNodeMut<'_> {
3737
value_offset: prop.value_offset,
3838
len: prop.len,
3939
nameoff: prop.nameoff,
40-
data: self.data,
40+
data: self.data.reborrow(),
4141
});
4242
}
4343
}
@@ -48,7 +48,7 @@ impl FdtNodeMut<'_> {
4848
pub fn properties_mut(&mut self) -> FdtPropMutIter<'_> {
4949
FdtPropMutIter {
5050
inner: InnerPropIter::new(self.offset),
51-
data: self.data,
51+
data: self.data.reborrow(),
5252
}
5353
}
5454

@@ -58,14 +58,14 @@ impl FdtNodeMut<'_> {
5858
private::FdtChildMutIter {
5959
inner: InnerChildIter::new(self.offset),
6060
parent_address_space: address_space,
61-
data: self.data,
61+
data: self.data.reborrow(),
6262
}
6363
}
6464

6565
/// Returns a read only view of this node.
6666
#[must_use]
6767
pub fn as_read_only(&self) -> FdtNode<'_> {
68-
let fdt = Fdt { data: &*self.data };
68+
let fdt = self.data.as_read_only();
6969
FdtNode {
7070
fdt,
7171
offset: self.offset,
@@ -120,15 +120,14 @@ impl Node for FdtNodeMut<'_> {
120120
}
121121

122122
mod private {
123-
use crate::fdt::Fdt;
124123
use crate::fdt::node::InnerChildIter;
125-
use crate::fdt_mut::FdtNodeMut;
124+
use crate::fdt_mut::{FdtMut, FdtNodeMut};
126125
use crate::standard::AddressSpaceProperties;
127126

128127
/// A mutable iterator over the children of a device tree node.
129128
#[derive(Debug)]
130129
pub struct FdtChildMutIter<'a> {
131-
pub(crate) data: &'a mut [u8],
130+
pub(crate) data: FdtMut<'a>,
132131
pub(crate) parent_address_space: AddressSpaceProperties,
133132
pub(crate) inner: InnerChildIter,
134133
}
@@ -140,12 +139,12 @@ mod private {
140139
///
141140
/// Panics if the underlying device tree data is invalid.
142141
pub fn next(&mut self) -> Option<FdtNodeMut<'_>> {
143-
let fdt = Fdt::new_unchecked(&*self.data);
142+
let fdt = self.data.as_read_only();
144143
let node_offset = self.inner.next(fdt)?;
145144
Some(FdtNodeMut {
146145
offset: node_offset,
147146
parent_address_space: self.parent_address_space,
148-
data: self.data,
147+
data: self.data.reborrow(),
149148
})
150149
}
151150
}

src/fdt_mut/property.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ use crate::Property;
1515
use crate::error::FdtMutError;
1616
use crate::fdt::property::InnerPropIter;
1717
use crate::fdt::{FDT_TAGSIZE, Fdt, FdtProperty};
18+
use crate::fdt_mut::FdtMut;
1819

1920
/// A mutable property of a device tree node.
2021
#[derive(Debug)]
2122
pub struct FdtPropertyMut<'a> {
22-
pub(crate) data: &'a mut [u8],
23+
pub(crate) data: FdtMut<'a>,
2324
pub(crate) nameoff: usize,
2425
pub(crate) prop_offset: usize,
2526
pub(crate) value_offset: usize,
@@ -64,7 +65,7 @@ impl FdtPropertyMut<'_> {
6465

6566
// Update the length in the FDT property header
6667
let (len_bytes, _) =
67-
<big_endian::U32>::mut_from_prefix(&mut self.data[self.prop_offset + FDT_TAGSIZE..])
68+
<big_endian::U32>::mut_from_prefix(&mut self.data.data[self.prop_offset + FDT_TAGSIZE..])
6869
.expect("Fdt should be valid");
6970
len_bytes.set(
7071
new_value
@@ -74,12 +75,12 @@ impl FdtPropertyMut<'_> {
7475
);
7576

7677
// Copy the new value
77-
self.data[self.value_offset..self.value_offset + new_value.len()]
78+
self.data.data[self.value_offset..self.value_offset + new_value.len()]
7879
.copy_from_slice(new_value);
7980

8081
// Zero out any padding bytes
8182
for i in new_value.len()..new_padded {
82-
self.data[self.value_offset + i] = 0;
83+
self.data.data[self.value_offset + i] = 0;
8384
}
8485

8586
self.len = new_value.len();
@@ -90,7 +91,7 @@ impl FdtPropertyMut<'_> {
9091
/// Returns the value of this property.
9192
#[must_use]
9293
pub fn value(&self) -> &[u8] {
93-
&self.data[self.value_offset..self.value_offset + self.len]
94+
&self.data.data[self.value_offset..self.value_offset + self.len]
9495
}
9596

9697
/// Returns a read only view of this property.
@@ -100,7 +101,7 @@ impl FdtPropertyMut<'_> {
100101
/// Panics if the underlying device tree data is invalid.
101102
#[must_use]
102103
pub fn as_read_only(&self) -> FdtProperty<'_> {
103-
let fdt = Fdt { data: &*self.data };
104+
let fdt = self.data.as_read_only();
104105
let name = fdt.string(self.nameoff).expect("Fdt should be valid");
105106
let value = fdt
106107
.data
@@ -123,12 +124,12 @@ impl<'a> Property for &'a FdtPropertyMut<'_> {
123124
type CellsItem = crate::Cells<'a>;
124125

125126
fn name(&self) -> &str {
126-
let fdt = Fdt { data: &*self.data };
127+
let fdt = self.data.as_read_only();
127128
fdt.string(self.nameoff).expect("Fdt should be valid")
128129
}
129130

130131
fn value(&self) -> &[u8] {
131-
let fdt = Fdt { data: &*self.data };
132+
let fdt = self.data.as_read_only();
132133
fdt.data
133134
.get(self.value_offset..self.value_offset + self.len)
134135
.expect("Fdt should be valid")
@@ -157,7 +158,7 @@ impl<'a> Property for &'a FdtPropertyMut<'_> {
157158
/// A mutable iterator over the properties of a device tree node.
158159
#[derive(Debug)]
159160
pub struct FdtPropMutIter<'a> {
160-
pub(crate) data: &'a mut [u8],
161+
pub(crate) data: FdtMut<'a>,
161162
pub(crate) inner: InnerPropIter,
162163
}
163164

@@ -168,14 +169,14 @@ impl FdtPropMutIter<'_> {
168169
///
169170
/// Panics if the underlying device tree data is invalid.
170171
pub fn next(&mut self) -> Option<FdtPropertyMut<'_>> {
171-
let fdt = Fdt::new_unchecked(&*self.data);
172+
let fdt = self.data.as_read_only();
172173
let parsed = self.inner.next(fdt)?;
173174
Some(FdtPropertyMut {
174175
prop_offset: parsed.prop_offset,
175176
value_offset: parsed.value_offset,
176177
len: parsed.len,
177178
nameoff: parsed.nameoff,
178-
data: self.data,
179+
data: self.data.reborrow(),
179180
})
180181
}
181182
}

0 commit comments

Comments
 (0)