Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make read_exact_into function unsafe #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions w3b-abi/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ fn encode_token_into(token: &Token, out: &mut String) {
use Token::*;

match token {
Int(int) => hex::read_exact_into(int.as_bytes(), out),
Uint(uint) => hex::read_exact_into(uint.as_bytes(), out),
Int(int) => unsafe { hex::read_exact_into(int.as_bytes(), out) },
Uint(uint) => unsafe { hex::read_exact_into(uint.as_bytes(), out) },
Bool(bool) => hex::read_left_padded_into(&[*bool as u8], 32, out),
Address(address) => hex::read_left_padded_into(address.as_bytes(), 32, out),

Expand Down
18 changes: 8 additions & 10 deletions w3b-types-core/src/hex/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn read_right_padded(bytes: &[u8], max_byte_len: usize) -> String {
#[inline]
pub fn read_exact(bytes: &[u8]) -> String {
let mut hex = String::from("0x");
unprefixed::read_exact_into(bytes, &mut hex);
unsafe { unprefixed::read_exact_into(bytes, &mut hex) };
hex
}

Expand Down Expand Up @@ -200,7 +200,7 @@ pub mod unprefixed {
#[inline]
pub fn read_exact(bytes: &[u8]) -> String {
let mut hex = String::new();
read_exact_into(bytes, &mut hex);
unsafe { read_exact_into(bytes, &mut hex) };
hex
}

Expand All @@ -218,7 +218,7 @@ pub mod unprefixed {
bytes = &bytes[1..];
}

read_exact_into(bytes, hex);
unsafe { read_exact_into(bytes, hex) };
} else {
hex.push('0');
}
Expand All @@ -228,23 +228,21 @@ pub mod unprefixed {
pub fn read_left_padded_into(bytes: &[u8], max_byte_len: usize, hex: &mut String) {
assert!(bytes.len() <= max_byte_len, "maximum byte length exceeded");
pad_into(max_byte_len - bytes.len(), hex);
read_exact_into(bytes, hex);
unsafe { read_exact_into(bytes, hex) };
}

#[inline]
pub fn read_right_padded_into(bytes: &[u8], max_byte_len: usize, hex: &mut String) {
assert!(bytes.len() <= max_byte_len, "maximum byte length exceeded");
read_exact_into(bytes, hex);
unsafe { read_exact_into(bytes, hex) };
pad_into(max_byte_len - bytes.len(), hex);
}

#[inline]
pub fn read_exact_into(bytes: &[u8], hex: &mut String) {
pub unsafe fn read_exact_into(bytes: &[u8], hex: &mut String) {
for byte in bytes {
unsafe {
hex.as_mut_vec().push(HEX_CHARS[(byte >> 4) as usize]);
hex.as_mut_vec().push(HEX_CHARS[(byte & 0xf) as usize]);
}
hex.as_mut_vec().push(HEX_CHARS[(byte >> 4) as usize]);
hex.as_mut_vec().push(HEX_CHARS[(byte & 0xf) as usize]);
}
}

Expand Down