Skip to content

Commit 9a7d216

Browse files
committed
Make RegValue.bytes a Cow<[u8]> instead of Vec<u8>
1 parent 5f96ef9 commit 9a7d216

File tree

6 files changed

+26
-26
lines changed

6 files changed

+26
-26
lines changed

src/decoder/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Decoder {
109109
.key
110110
.get_raw_value(name)
111111
.map_err(DecoderError::IoError)?;
112-
Ok(bytes)
112+
Ok(bytes.into_owned())
113113
}
114114
_ => Err(DecoderError::DeserializerError("Not a value".to_owned())),
115115
}

src/decoder/serialization_serde.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl<'de> Deserializer<'de> for &mut Decoder {
3535
}
3636
REG_DWORD => visitor.visit_u32(u32::from_reg_value(&v)?),
3737
REG_QWORD => visitor.visit_u64(u64::from_reg_value(&v)?),
38-
REG_BINARY => visitor.visit_byte_buf(v.bytes),
38+
REG_BINARY => visitor.visit_byte_buf(v.bytes.into_owned()),
3939
REG_NONE => visitor.visit_none(),
4040
_ => no_impl!(format!(
4141
"value type deserialization not implemented {:?}",
@@ -180,20 +180,19 @@ impl<'de> Deserializer<'de> for &mut Decoder {
180180
use super::DecoderCursor::*;
181181
match self.cursor {
182182
Start => return visitor.visit_some(&mut *self),
183-
FieldVal(index, ref name) => self
184-
.key
185-
.get_raw_value(name)
186-
.map_err(DecoderError::IoError)
187-
.and_then(|v| match v {
188-
RegValue {
183+
FieldVal(index, ref name) => {
184+
let v = self.key.get_raw_value(name).map_err(DecoderError::IoError);
185+
match v {
186+
Ok(RegValue {
189187
vtype: crate::enums::RegType::REG_NONE,
190188
..
191-
} => {
189+
}) => {
192190
self.cursor = DecoderCursor::Field(index + 1);
193191
Err(DecoderError::DeserializerError("Found REG_NONE".to_owned()))
194192
}
195193
val => Ok(val),
196-
}),
194+
}
195+
}
197196
_ => Err(DecoderError::DeserializerError("Nothing found".to_owned())),
198197
}
199198
};

src/encoder/serialization_serde.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, Tr: AsRef<Transaction>> Serializer for &'a mut Encoder<Tr> {
9595
.set_raw_value(
9696
s,
9797
&RegValue {
98-
bytes: vec,
98+
bytes: vec.into(),
9999
vtype: RegType::REG_BINARY,
100100
},
101101
)

src/reg_key.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ impl RegKey {
687687
}
688688
let t: RegType = unsafe { transmute(buf_type as u8) };
689689
return Ok(RegValue {
690-
bytes: buf,
690+
bytes: buf.into(),
691691
vtype: t,
692692
});
693693
}
@@ -734,7 +734,7 @@ impl RegKey {
734734
/// let hkcu = RegKey::predef(HKEY_CURRENT_USER);
735735
/// let settings = hkcu.open_subkey("Software\\MyProduct\\Settings")?;
736736
/// let bytes: Vec<u8> = vec![1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
737-
/// let data = RegValue{ vtype: REG_BINARY, bytes: bytes};
737+
/// let data = RegValue{ vtype: REG_BINARY, bytes: bytes.into()};
738738
/// settings.set_raw_value("data", &data)?;
739739
/// println!("Bytes: {:?}", data.bytes);
740740
/// # Ok(())
@@ -992,7 +992,7 @@ impl RegKey {
992992
}
993993
let t: RegType = unsafe { transmute(buf_type as u8) };
994994
let value = RegValue {
995-
bytes: buf,
995+
bytes: buf.into(),
996996
vtype: t,
997997
};
998998
return Some(Ok((name, value)));
@@ -1045,10 +1045,10 @@ pub struct EnumValues<'key> {
10451045
index: DWORD,
10461046
}
10471047

1048-
impl Iterator for EnumValues<'_> {
1049-
type Item = io::Result<(String, RegValue)>;
1048+
impl<'a> Iterator for EnumValues<'a> {
1049+
type Item = io::Result<(String, RegValue<'a>)>;
10501050

1051-
fn next(&mut self) -> Option<io::Result<(String, RegValue)>> {
1051+
fn next(&mut self) -> Option<io::Result<(String, RegValue<'a>)>> {
10521052
match self.key.enum_value(self.index) {
10531053
v @ Some(_) => {
10541054
self.index += 1;

src/reg_value.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
// except according to those terms.
66
use crate::enums::*;
77
use crate::types::FromRegValue;
8+
use std::borrow::Cow;
89
use std::fmt;
910

1011
/// Raw registry value
1112
#[derive(PartialEq)]
12-
pub struct RegValue {
13-
pub bytes: Vec<u8>,
13+
pub struct RegValue<'a> {
14+
pub bytes: Cow<'a, [u8]>,
1415
pub vtype: RegType,
1516
}
1617

@@ -23,7 +24,7 @@ macro_rules! format_reg_value {
2324
};
2425
}
2526

26-
impl fmt::Display for RegValue {
27+
impl fmt::Display for RegValue<'_> {
2728
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2829
let f_val = match self.vtype {
2930
REG_SZ | REG_EXPAND_SZ | REG_MULTI_SZ => format_reg_value!(self => String),
@@ -35,7 +36,7 @@ impl fmt::Display for RegValue {
3536
}
3637
}
3738

38-
impl fmt::Debug for RegValue {
39+
impl fmt::Debug for RegValue<'_> {
3940
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4041
write!(f, "RegValue({:?}: {})", self.vtype, self)
4142
}

src/types.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl FromRegValue for Vec<OsString> {
111111
macro_rules! try_from_reg_value_int {
112112
($val:expr, $map:expr) => {
113113
$val.bytes
114-
.as_slice()
114+
.as_ref()
115115
.try_into()
116116
.map($map)
117117
.map_err(|_| io::Error::from_raw_os_error(winerror::ERROR_INVALID_DATA as i32))
@@ -149,7 +149,7 @@ macro_rules! to_reg_value_sz {
149149
impl<$($l,)*> ToRegValue for $t {
150150
fn to_reg_value(&self) -> RegValue {
151151
RegValue {
152-
bytes: v16_to_v8(&to_utf16(self)),
152+
bytes: v16_to_v8(&to_utf16(self)).into(),
153153
vtype: REG_SZ,
154154
}
155155
}
@@ -173,7 +173,7 @@ macro_rules! to_reg_value_multi_sz {
173173
.concat();
174174
os_strings.push(0);
175175
RegValue {
176-
bytes: v16_to_v8(&os_strings),
176+
bytes: v16_to_v8(&os_strings).into(),
177177
vtype: REG_MULTI_SZ,
178178
}
179179
}
@@ -191,7 +191,7 @@ impl ToRegValue for u32 {
191191
let bytes: Vec<u8> =
192192
unsafe { slice::from_raw_parts((self as *const u32) as *const u8, 4).to_vec() };
193193
RegValue {
194-
bytes,
194+
bytes: bytes.into(),
195195
vtype: REG_DWORD,
196196
}
197197
}
@@ -202,7 +202,7 @@ impl ToRegValue for u64 {
202202
let bytes: Vec<u8> =
203203
unsafe { slice::from_raw_parts((self as *const u64) as *const u8, 8).to_vec() };
204204
RegValue {
205-
bytes,
205+
bytes: bytes.into(),
206206
vtype: REG_QWORD,
207207
}
208208
}

0 commit comments

Comments
 (0)