Skip to content

Commit

Permalink
Make RegValue.bytes a Cow<[u8]> instead of Vec<u8>
Browse files Browse the repository at this point in the history
  • Loading branch information
gentoo90 committed Jan 6, 2025
1 parent cb06180 commit e54abbd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Decoder {
.key
.get_raw_value(name)
.map_err(DecoderError::IoError)?;
Ok(bytes)
Ok(bytes.into_owned())
}
_ => Err(DecoderError::DeserializerError("Not a value".to_owned())),
}
Expand Down
17 changes: 8 additions & 9 deletions src/decoder/serialization_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'de> Deserializer<'de> for &mut Decoder {
}
REG_DWORD => visitor.visit_u32(u32::from_reg_value(&v)?),
REG_QWORD => visitor.visit_u64(u64::from_reg_value(&v)?),
REG_BINARY => visitor.visit_byte_buf(v.bytes),
REG_BINARY => visitor.visit_byte_buf(v.bytes.into_owned()),
REG_NONE => visitor.visit_none(),
_ => no_impl!(format!(
"value type deserialization not implemented {:?}",
Expand Down Expand Up @@ -180,20 +180,19 @@ impl<'de> Deserializer<'de> for &mut Decoder {
use super::DecoderCursor::*;
match self.cursor {
Start => return visitor.visit_some(&mut *self),
FieldVal(index, ref name) => self
.key
.get_raw_value(name)
.map_err(DecoderError::IoError)
.and_then(|v| match v {
RegValue {
FieldVal(index, ref name) => {
let v = self.key.get_raw_value(name).map_err(DecoderError::IoError);
match v {
Ok(RegValue {
vtype: crate::enums::RegType::REG_NONE,
..
} => {
}) => {
self.cursor = DecoderCursor::Field(index + 1);
Err(DecoderError::DeserializerError("Found REG_NONE".to_owned()))
}
val => Ok(val),
}),
}
}
_ => Err(DecoderError::DeserializerError("Nothing found".to_owned())),
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/encoder/serialization_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a, Tr: AsRef<Transaction>> Serializer for &'a mut Encoder<Tr> {
.set_raw_value(
s,
&RegValue {
bytes: vec,
bytes: vec.into(),
vtype: RegType::REG_BINARY,
},
)
Expand Down
12 changes: 6 additions & 6 deletions src/reg_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ impl RegKey {
}
let t: RegType = unsafe { transmute(buf_type as u8) };
return Ok(RegValue {
bytes: buf,
bytes: buf.into(),
vtype: t,
});
}
Expand Down Expand Up @@ -734,7 +734,7 @@ impl RegKey {
/// let hkcu = RegKey::predef(HKEY_CURRENT_USER);
/// let settings = hkcu.open_subkey("Software\\MyProduct\\Settings")?;
/// let bytes: Vec<u8> = vec![1, 2, 3, 5, 8, 13, 21, 34, 55, 89];
/// let data = RegValue{ vtype: REG_BINARY, bytes: bytes};
/// let data = RegValue{ vtype: REG_BINARY, bytes: bytes.into()};
/// settings.set_raw_value("data", &data)?;
/// println!("Bytes: {:?}", data.bytes);
/// # Ok(())
Expand Down Expand Up @@ -992,7 +992,7 @@ impl RegKey {
}
let t: RegType = unsafe { transmute(buf_type as u8) };
let value = RegValue {
bytes: buf,
bytes: buf.into(),
vtype: t,
};
return Some(Ok((name, value)));
Expand Down Expand Up @@ -1045,10 +1045,10 @@ pub struct EnumValues<'key> {
index: DWORD,
}

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

fn next(&mut self) -> Option<io::Result<(String, RegValue)>> {
fn next(&mut self) -> Option<io::Result<(String, RegValue<'a>)>> {
match self.key.enum_value(self.index) {
v @ Some(_) => {
self.index += 1;
Expand Down
9 changes: 5 additions & 4 deletions src/reg_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// except according to those terms.
use crate::enums::*;
use crate::types::FromRegValue;
use std::borrow::Cow;
use std::fmt;

/// Raw registry value
#[derive(PartialEq)]
pub struct RegValue {
pub bytes: Vec<u8>,
pub struct RegValue<'a> {
pub bytes: Cow<'a, [u8]>,
pub vtype: RegType,
}

Expand All @@ -23,7 +24,7 @@ macro_rules! format_reg_value {
};
}

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

impl fmt::Debug for RegValue {
impl fmt::Debug for RegValue<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "RegValue({:?}: {})", self.vtype, self)
}
Expand Down
10 changes: 5 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl FromRegValue for Vec<OsString> {
macro_rules! try_from_reg_value_int {
($val:expr, $map:expr) => {
$val.bytes
.as_slice()
.as_ref()
.try_into()
.map($map)
.map_err(|_| io::Error::from_raw_os_error(winerror::ERROR_INVALID_DATA as i32))
Expand Down Expand Up @@ -149,7 +149,7 @@ macro_rules! to_reg_value_sz {
impl<$($l,)*> ToRegValue for $t {
fn to_reg_value(&self) -> RegValue {
RegValue {
bytes: v16_to_v8(&to_utf16(self)),
bytes: v16_to_v8(&to_utf16(self)).into(),
vtype: REG_SZ,
}
}
Expand All @@ -173,7 +173,7 @@ macro_rules! to_reg_value_multi_sz {
.concat();
os_strings.push(0);
RegValue {
bytes: v16_to_v8(&os_strings),
bytes: v16_to_v8(&os_strings).into(),
vtype: REG_MULTI_SZ,
}
}
Expand All @@ -191,7 +191,7 @@ impl ToRegValue for u32 {
let bytes: Vec<u8> =
unsafe { slice::from_raw_parts((self as *const u32) as *const u8, 4).to_vec() };
RegValue {
bytes,
bytes: bytes.into(),
vtype: REG_DWORD,
}
}
Expand All @@ -202,7 +202,7 @@ impl ToRegValue for u64 {
let bytes: Vec<u8> =
unsafe { slice::from_raw_parts((self as *const u64) as *const u8, 8).to_vec() };
RegValue {
bytes,
bytes: bytes.into(),
vtype: REG_QWORD,
}
}
Expand Down

0 comments on commit e54abbd

Please sign in to comment.