Skip to content

Commit

Permalink
data
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Aug 1, 2024
1 parent 76ed3b5 commit a39755d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::*;

// Minimal `Vec<u8>` replacement providing at least `u16` alignment so that it can be used for wide strings.
pub struct ValueBytes {
pub struct Data {
ptr: *mut u8,
len: usize,
}

impl ValueBytes {
impl Data {
// Creates a buffer with the specified length of zero bytes.
pub fn new(len: usize) -> Result<Self> {
unsafe {
Expand Down Expand Up @@ -69,7 +69,7 @@ impl ValueBytes {
}
}

impl Drop for ValueBytes {
impl Drop for Data {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe {
Expand All @@ -79,7 +79,7 @@ impl Drop for ValueBytes {
}
}

impl Deref for ValueBytes {
impl Deref for Data {
type Target = [u8];

fn deref(&self) -> &[u8] {
Expand All @@ -91,7 +91,7 @@ impl Deref for ValueBytes {
}
}

impl core::ops::DerefMut for ValueBytes {
impl core::ops::DerefMut for Data {
fn deref_mut(&mut self) -> &mut [u8] {
if self.ptr.is_null() {
&mut []
Expand All @@ -101,27 +101,27 @@ impl core::ops::DerefMut for ValueBytes {
}
}

impl Clone for ValueBytes {
impl Clone for Data {
fn clone(&self) -> Self {
Self::from_slice(self).unwrap()
}
}

impl PartialEq for ValueBytes {
impl PartialEq for Data {
fn eq(&self, other: &Self) -> bool {
self.deref() == other.deref()
}
}

impl Eq for ValueBytes {}
impl Eq for Data {}

impl core::fmt::Debug for ValueBytes {
impl core::fmt::Debug for Data {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.deref().fmt(f)
}
}

impl<const N: usize> TryFrom<[u8; N]> for ValueBytes {
impl<const N: usize> TryFrom<[u8; N]> for Data {
type Error = Error;
fn try_from(from: [u8; N]) -> Result<Self> {
Self::from_slice(&from)
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/registry/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ impl Key {
pub fn get_value<T: AsRef<str>>(&self, name: T) -> Result<Value> {
let name = pcwstr(name);
let (ty, len) = unsafe { self.raw_get_info(name.as_raw())? };
let mut value = ValueBytes::new(len)?;
unsafe { self.raw_get_bytes(name.as_raw(), &mut value)? };
Ok(Value { value, ty })
let mut data = Data::new(len)?;
unsafe { self.raw_get_bytes(name.as_raw(), &mut data)? };
Ok(Value { data, ty })
}

/// Gets the value for the name in the registry key.
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub use key::Key;
mod value;
pub use value::Value;

mod value_bytes;
use value_bytes::ValueBytes;
mod data;
use data::Data;

mod pcwstr;
use pcwstr::*;
Expand Down
16 changes: 8 additions & 8 deletions crates/libs/registry/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
/// A registry value.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Value {
pub(crate) value: ValueBytes,
pub(crate) data: Data,
pub(crate) ty: Type,
}

Expand All @@ -23,21 +23,21 @@ impl core::ops::Deref for Value {
type Target = [u8];

fn deref(&self) -> &[u8] {
&self.value
&self.data
}
}

impl AsRef<[u8]> for Value {
fn as_ref(&self) -> &[u8] {
&self.value
&self.data
}
}

impl TryFrom<u32> for Value {
type Error = Error;
fn try_from(from: u32) -> Result<Self> {
Ok(Self {
value: from.to_le_bytes().try_into()?,
data: from.to_le_bytes().try_into()?,
ty: Type::U32,
})
}
Expand All @@ -54,7 +54,7 @@ impl TryFrom<u64> for Value {
type Error = Error;
fn try_from(from: u64) -> Result<Self> {
Ok(Self {
value: from.to_le_bytes().try_into()?,
data: from.to_le_bytes().try_into()?,
ty: Type::U64,
})
}
Expand All @@ -71,7 +71,7 @@ impl TryFrom<Value> for String {
type Error = Error;
fn try_from(from: Value) -> Result<Self> {
match from.ty {
Type::String | Type::ExpandString => Ok(Self::from_utf16(from.value.as_wide())?),
Type::String | Type::ExpandString => Ok(Self::from_utf16(from.data.as_wide())?),
_ => Err(invalid_data()),
}
}
Expand All @@ -81,7 +81,7 @@ impl TryFrom<&str> for Value {
type Error = Error;
fn try_from(from: &str) -> Result<Self> {
Ok(Self {
value: ValueBytes::from_slice(pcwstr(from).as_bytes())?,
data: Data::from_slice(pcwstr(from).as_bytes())?,
ty: Type::String,
})
}
Expand All @@ -92,7 +92,7 @@ impl TryFrom<Value> for Vec<String> {
fn try_from(from: Value) -> Result<Self> {
match from.ty {
Type::MultiString => Ok(from
.value
.data
.as_wide()
.split(|c| *c == 0)
.map(String::from_utf16_lossy)
Expand Down
12 changes: 6 additions & 6 deletions crates/libs/registry/src/value_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct ValueIterator<'a> {
key: &'a Key,
range: core::ops::Range<usize>,
name: Vec<u16>,
value: ValueBytes,
data: Data,
}

impl<'a> ValueIterator<'a> {
Expand Down Expand Up @@ -37,7 +37,7 @@ impl<'a> ValueIterator<'a> {
key,
range: 0..count as usize,
name: vec![0; name_max_len as usize + 1],
value: ValueBytes::new(value_max_len as usize)?,
data: Data::new(value_max_len as usize)?,
})
}
}
Expand All @@ -49,7 +49,7 @@ impl<'a> Iterator for ValueIterator<'a> {
self.range.next().and_then(|index| {
let mut ty = 0;
let mut name_len = self.name.len() as u32;
let mut value_len = self.value.len() as u32;
let mut data_len = self.data.len() as u32;

let result = unsafe {
RegEnumValueW(
Expand All @@ -59,8 +59,8 @@ impl<'a> Iterator for ValueIterator<'a> {
&mut name_len,
core::ptr::null(),
&mut ty,
self.value.as_mut_ptr(),
&mut value_len,
self.data.as_mut_ptr(),
&mut data_len,
)
};

Expand All @@ -72,7 +72,7 @@ impl<'a> Iterator for ValueIterator<'a> {
Some((
name,
Value {
value: ValueBytes::from_slice(&self.value[0..value_len as usize]).unwrap(),
data: Data::from_slice(&self.data[0..data_len as usize]).unwrap(),
ty: ty.into(),
},
))
Expand Down

0 comments on commit a39755d

Please sign in to comment.