get rid of most primitive casts

png-branch
an 2019-03-02 21:31:00 -05:00
parent a7fb392c5e
commit 4570d494ad
10 changed files with 106 additions and 112 deletions

View File

@ -19,14 +19,6 @@ macro_rules! _durandal_read_impl {
(BE $b:ident $nam:ident i32 $n:expr) => {
_durandal_read_impl!($b i32::from_be_bytes, $nam 4 $n);
};
(BE $b:ident $nam:ident as usize u16 $n:expr) => {
_durandal_read_impl!($b u16::from_be_bytes, $nam 2 $n);
let $nam = $nam as usize;
};
(BE $b:ident $nam:ident as usize u32 $n:expr) => {
_durandal_read_impl!($b u32::from_be_bytes, $nam 4 $n);
let $nam = $nam as usize;
};
// little endian
(LE $b:ident $nam:ident u16 $n:expr) => {
@ -41,14 +33,6 @@ macro_rules! _durandal_read_impl {
(LE $b:ident $nam:ident i32 $n:expr) => {
_durandal_read_impl!($b i32::from_le_bytes $nam 4 $n);
};
(LE $b:ident $nam:ident as usize u16 $n:expr) => {
_durandal_read_impl!($b u16::from_le_bytes $nam 2 $n);
let $nam = $nam as usize;
};
(LE $b:ident $nam:ident as usize u32 $n:expr) => {
_durandal_read_impl!($b u32::from_le_bytes $nam 4 $n);
let $nam = $nam as usize;
};
// either endianness
($e:ident $b:ident $nam:ident Angle $n:expr) => {
@ -67,16 +51,24 @@ macro_rules! _durandal_read_impl {
_durandal_read_impl!($e $b $nam u16 $n);
let $nam = OptU16::from_repr($nam);
};
($e:ident $b:ident $nam:ident usize u16 $n:expr) => {
_durandal_read_impl!($e $b $nam u16 $n);
let $nam = usize::from($nam);
};
($e:ident $b:ident $nam:ident usize u32 $n:expr) => {
_durandal_read_impl!($e $b $nam u32 $n);
let $nam = usize_from_u32($nam);
};
// generic endianness
// no endianness
($_:ident $b:ident $nam:ident u8 $n:expr) => {
let $nam = $b[$n];
};
($_:ident $b:ident $nam:ident array u8 $n:expr) => {
($_:ident $b:ident $nam:ident slice u8 $n:expr) => {
let $nam = &$b[$n];
};
($_:ident $b:ident $nam:ident i8 $n:expr) => {
let $nam = $b[$n] as i8;
let $nam = i8::from_ne_bytes([$b[$n]]);
};
($_:ident $b:ident $nam:ident Ident $n:expr) => {
let $nam = Ident([$b[$n], $b[$n + 1], $b[$n + 2], $b[$n + 3]]);
@ -84,7 +76,7 @@ macro_rules! _durandal_read_impl {
($_:ident $b:ident $nam:ident $f:ident $n:expr) => {
let $nam = $f(&$b[$n])?;
};
($_:ident $b:ident $nam:ident nt $f:ident $n:expr) => {
($_:ident $b:ident $nam:ident no_try $f:ident $n:expr) => {
let $nam = $f(&$b[$n]);
};
@ -97,7 +89,7 @@ macro_rules! _durandal_read_impl {
};
}
/// Reads structured data from a byte array.
/// Reads structured data from a byte slice.
///
/// First start by specifying the endianness, size and source using the syntax
/// `endian, size in source =>` where:
@ -128,12 +120,12 @@ macro_rules! _durandal_read_impl {
/// - The name of a function, which is passed `&source[place]` as its only
/// argument. The function's result has the `?` operator applied to it.
/// - `opts` may be one of:
/// - `array` when `type` is `u8`: `place` is a range specifying a `u8` slice
/// - `slice` when `type` is `u8`: `place` is a range specifying a `u8` slice
/// to be taken from `source`.
/// - `as usize` when `type` is `u16` or `u32`: converts the resulting
/// integer to `usize` by primitive cast.
/// - `nt` when `type` is a function name: does not use the `?` operator on
/// the resulting function call
/// - `usize` when `type` is `u16` or `u32`: converts the resulting integer
/// to `usize` by `usize_to_u32` for `u32` or by `from` for `u16`.
/// - `no_try` when `type` is a function name: does not use the `?` operator
/// on the resulting function call.
/// - Nothing at all.
/// - `place` is either an integer literal which must be representable as
/// `usize`, or a range, which may only be used when `type` is a function
@ -152,6 +144,10 @@ macro_rules! read_data {
};
}
/// Casts a `u32` to a `usize`. For future compatibility.
#[inline]
pub const fn usize_from_u32(n: u32) -> usize {n as usize}
/// Creates an `Ident` from a slice.
///
/// `b` must be at least 4 bytes, or a panic will occur.
@ -217,7 +213,7 @@ pub fn rd_ofstable<T, F>(b: &[u8],
let mut v = Vec::with_capacity(num);
for _ in 0..num {
let ofs = u32b(&b[p..p + 4]) as usize;
let ofs = usize_from_u32(u32b(&b[p..p + 4]));
if ofs >= b.len() {
bail!("not enough data");

View File

@ -13,7 +13,7 @@ fn crc_init() -> [u32; 256]
{
let mut t = [0; 256];
for (n, v) in t.iter_mut().enumerate() {
*v = (0..8).fold(n as u32, crc_accum);
*v = (0..8).fold(u32::from(n), crc_accum);
}
t
}
@ -22,8 +22,7 @@ fn crc_init() -> [u32; 256]
pub fn crc32(b: &[u8], s: u32) -> u32
{
let t = crc_init();
!b.iter()
.fold(s, |a, &o| a >> 8 ^ t[(a & 0xFF ^ u32::from(o)) as usize])
!b.iter().fold(s, |a, &o| a >> 8 ^ t[usize::from(a as u8 ^ o)])
}
#[test]

View File

@ -35,9 +35,9 @@ pub fn to_binsize(n: u64) -> String
// terabytes, gigabytes, megabytes, kilobytes
for i in (1..=4).rev() {
let pow = 1000_u64.pow(i);
let pow = 1000_u64.pow(i as u32);
if n >= pow {
return format!("{:1}{}", n / pow, NAMES[i as usize - 1]);
return format!("{:1}{}", n / pow, NAMES[i - 1]);
}
}
@ -70,7 +70,7 @@ pub fn fuck_string(s: &[u8]) -> Vec<u8>
/// Reads a Pascal-style byte string with bounds checking.
pub fn pascal_str(b: &[u8]) -> Option<&[u8]>
{
let s = *b.get(0)? as usize;
let s = usize::from(*b.get(0)?);
b.get(1..=s)
}
@ -80,13 +80,12 @@ pub fn mac_roman_conv(s: &[u8]) -> String
let mut v = String::with_capacity(s.len());
for &c in s.iter() {
let c = match c {
v.push(match c {
0 => break,
b'\r' => '\n',
c if c & 0x80 != 0 => TR[c as usize & 0x7F],
c => c as char,
};
v.push(c);
c if c & 0x80 != 0 => TR[usize::from(c) & 0x7F],
c => char::from(c),
});
}
v

View File

@ -10,7 +10,7 @@ pub fn check_apple_single(b: &[u8]) -> Option<usize>
return None;
}
let num = u16b(&b[24..]) as usize;
let num = usize::from(u16b(&b[24..]));
if b.len() < 26 + 12 * num {
return None;
@ -20,8 +20,8 @@ pub fn check_apple_single(b: &[u8]) -> Option<usize>
for i in 0..num {
let p = 26 + 12 * i;
let ent = u32b(&b[p..]);
let ofs = u32b(&b[p + 4..]) as usize;
let len = u32b(&b[p + 8..]) as usize;
let ofs = usize_from_u32(u32b(&b[p + 4..]));
let len = usize_from_u32(u32b(&b[p + 8..]));
if ent == 1 {
return if ofs + len > b.len() {None} else {Some(ofs)};

View File

@ -55,7 +55,7 @@ pub fn read_minf(b: &[u8]) -> ResultS<Minf>
music_id = u16[4];
missi_flags = u16[6];
envir_flags = u16[8];
level_name = mac_roman_conv[18..84] nt;
level_name = mac_roman_conv[18..84] no_try;
entry_flags = u32[84];
}
@ -232,7 +232,7 @@ pub fn read_old_lite(b: &[u8]) -> ResultS<(Light, usize)>
{
read_data! {
32, BE in b =>
ltype = u16[2] as usize;
ltype = u16[2] usize;
mode = u16[4];
phase = i16[6];
min = Fixed[8];
@ -400,7 +400,7 @@ pub fn read_note(b: &[u8]) -> ResultS<(Note, usize)>
72, BE in b =>
pos = read_point[2..6];
poly = u16[6];
text = mac_roman_conv[8..72] nt;
text = mac_roman_conv[8..72] no_try;
}
Ok((Note{pos, poly, text}, 72))

View File

@ -12,10 +12,10 @@ fn read_pm_header<'a>(b: &'a [u8],
read_data! {
36, BE in b =>
pt_fl = u16[0];
top = u16[2] as usize;
left = u16[4] as usize;
bottom = u16[6] as usize;
right = u16[8] as usize;
top = u16[2] usize;
left = u16[4] usize;
bottom = u16[6] usize;
right = u16[8] usize;
pack_t = u16[12];
depth = u16[28];
}
@ -44,14 +44,14 @@ fn read_pm_header<'a>(b: &'a [u8],
p += 18; // srcRect, dstRect, mode
if clip {
p += u16b(&b[p..]) as usize; // maskRgn
p += usize::from(u16b(&b[p..])); // maskRgn
}
let rle = pack_t == PackType::Default ||
pack_t == PackType::Rle16 && depth == 16 ||
pack_t == PackType::Rle32 && depth == 32;
let pitch = (pt_fl & 0x3FFF) as usize;
let pitch = usize::from(pt_fl & 0x3FFF);
Ok((&b[p..], Header{pitch, pack_t, depth, clut, rle}))
}
@ -66,7 +66,7 @@ fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
// uncompressed 8-bit colormap indices
for _ in 0..im.h() {
for _ in 0..im.w() {
let idx = b[p] as usize;
let idx = usize::from(b[p]);
im.cr.push(ok!(clut.get(idx), "invalid index")?.clone());
p += 1;
}
@ -87,7 +87,7 @@ fn read_pm_ind(mut im: Image8, b: &[u8], hdr: Header) -> ResultS<Image8>
for &idx in &d {
im.cr
.push(ok!(clut.get(idx as usize), "invalid index")?.clone());
.push(ok!(clut.get(usize::from(idx)), "invalid index")?.clone());
}
}
@ -203,8 +203,8 @@ pub fn load_pict(b: &[u8]) -> ResultS<Image8>
{
read_data! {
10, BE in b =>
h = u16[6] as usize;
w = u16[8] as usize;
h = u16[6] usize;
w = u16[8] usize;
}
let im = Image8::new(w, h);
@ -287,10 +287,10 @@ pub fn load_pict(b: &[u8]) -> ResultS<Image8>
0x0033 | // InvertRect
0x0034 => p += 8, // FillRect
0x002D => p += 10, // LineJustify
0x0001 => p += (u16b(&b[p.. ]) & !1) as usize, // Clip
0x00A1 => p += (u16b(&b[p+2..]) & !1) as usize + 2, // LongComment
0x0001 => p += usize::from(u16b(&b[p.. ]) & !1), // Clip
0x00A1 => p += usize::from(u16b(&b[p+2..]) & !1) + 2, // LongComment
0x100..=
0x7FFF => p += (op >> 8) as usize * 2, // Reserved
0x7FFF => p += usize::from(op >> 8) * 2, // Reserved
_ => {
bail!("invalid op in PICT");
}
@ -306,7 +306,7 @@ pub fn get_clut(b: &[u8]) -> ResultS<(Vec<Color8>, usize)>
read_data! {
8, BE in b =>
dev = u16[4];
num = u16[6] as usize;
num = u16[6] usize;
}
let dev = dev & 0x8000 != 0;
@ -318,7 +318,7 @@ pub fn get_clut(b: &[u8]) -> ResultS<(Vec<Color8>, usize)>
for i in 0..num {
read_data! {
p + 8, BE in b =>
n = u16[p] as usize;
n = u16[p] usize;
r = u8[p + 2];
g = u8[p + 4];
b = u8[p + 6];
@ -343,15 +343,15 @@ fn read_rle<T>(b: &[u8], pitch: usize) -> ResultS<(Vec<T>, usize)>
let mut o = Vec::with_capacity(pitch);
let sz = if pitch > 250 {
(u16b(b) as usize + 2, p += 2).0
(usize::from(u16b(b)) + 2, p += 2).0
} else {
(b[0] as usize + 1, p += 1).0
(usize::from(b[0]) + 1, p += 1).0
};
while p < sz {
let szf = b[p];
let cmp = szf & 0x80 != 0;
let len = if cmp {!szf + 2} else {szf + 1} as usize;
let len = usize::from(if cmp {!szf + 2} else {szf + 1});
p += 1;
o.reserve(len);

View File

@ -23,7 +23,7 @@ fn read_color(b: &[u8], clut: &mut [ColorShp]) -> ResultS<()>
}?;
let cr = ColorShp::Opaque{r, g, b, l};
clut[i as usize] = cr;
clut[usize::from(i)] = cr;
Ok(())
}
@ -56,8 +56,8 @@ pub fn read_bitmap(b: &[u8]) -> ResultS<Bitmap>
{
read_data! {
26, BE in b =>
width = u16[0] as usize;
height = u16[2] as usize;
width = u16[0] usize;
height = u16[2] usize;
compr = u16[4];
flags = u16[6];
depth = u16[8];
@ -83,8 +83,8 @@ pub fn read_bitmap(b: &[u8]) -> ResultS<Bitmap>
for _ in 0..scanlines {
read_data! {
p + 4, BE in b =>
fst = u16[p] as usize;
lst = u16[p + 2] as usize;
fst = u16[p] usize;
lst = u16[p + 2] usize;
}
let end = lst - fst;
@ -125,7 +125,7 @@ pub fn read_frame(b: &[u8]) -> ResultS<Frame>
36, BE in b =>
flags = u16[0];
min_lt = Fixed[2];
bmp_ind = u16[6] as usize;
bmp_ind = u16[6] usize;
wrl_l = Unit[16];
wrl_r = Unit[18];
wrl_t = Unit[20];
@ -144,7 +144,7 @@ pub fn read_sequence(b: &[u8]) -> ResultS<Sequence>
{
read_data! {
88, BE in b =>
name = u8[4..38] array;
name = u8[4..38] slice;
v_type = u16[38];
frames = u16[40];
ticks = u16[42];
@ -172,15 +172,15 @@ pub fn read_collection(b: &[u8]) -> ResultS<Collection>
544, BE in b =>
version = u16[0];
cl_type = u16[2];
clr_num = u16[6] as usize;
tab_num = u16[8] as usize;
tab_ofs = u32[10] as usize;
seq_num = u16[14] as usize;
seq_ofs = u32[16] as usize;
frm_num = u16[20] as usize;
frm_ofs = u32[22] as usize;
bmp_num = u16[26] as usize;
bmp_ofs = u32[28] as usize;
clr_num = u16[6] usize;
tab_num = u16[8] usize;
tab_ofs = u32[10] usize;
seq_num = u16[14] usize;
seq_ofs = u32[16] usize;
frm_num = u16[20] usize;
frm_ofs = u32[22] usize;
bmp_num = u16[26] usize;
bmp_ofs = u32[28] usize;
}
let cl_type = CollectionType::from_repr(cl_type)?;
@ -206,19 +206,19 @@ pub fn read_shapes(b: &[u8]) -> ResultS<Vec<CollectionDef>>
for _ in 0..32 {
read_data! {
p + 32, BE in b =>
lo_ofs = u32[p + 4] as usize;
lo_len = u32[p + 8] as usize;
hi_ofs = u32[p + 12] as usize;
hi_len = u32[p + 16] as usize;
lo_ofs = u32[p + 4] usize;
lo_len = u32[p + 8] usize;
hi_ofs = u32[p + 12] usize;
hi_len = u32[p + 16] usize;
}
let c_lo = if lo_ofs == u32::max_value() as usize {
let c_lo = if lo_ofs == usize_from_u32(u32::max_value()) {
None
} else {
Some(read_collection(&b[lo_ofs..lo_ofs + lo_len])?)
};
let c_hi = if hi_ofs == u32::max_value() as usize {
let c_hi = if hi_ofs == usize_from_u32(u32::max_value()) {
None
} else {
Some(read_collection(&b[hi_ofs..hi_ofs + hi_len])?)
@ -261,11 +261,11 @@ impl Image for ImageShp<'_, '_>
{
static TRANSLUCENT_COLOR: ColorShp = ColorShp::Translucent;
let cr = if self.bmp.cmajr {
self.bmp.cr[y + x * self.bmp.h] as usize
let cr = usize::from(if self.bmp.cmajr {
self.bmp.cr[y + x * self.bmp.h]
} else {
self.bmp.cr[x + y * self.bmp.w] as usize
};
self.bmp.cr[x + y * self.bmp.w]
});
if self.bmp.alpha && cr == 0 {
&TRANSLUCENT_COLOR

View File

@ -9,10 +9,10 @@ pub fn read_sound(b: &[u8]) -> ResultS<Sound16>
{
read_data! {
21, BE in b =>
len = u32[4] as usize;
len = u32[4] usize;
rate = u16[8];
lp_beg = u32[12] as usize;
lp_end = u32[16] as usize;
lp_beg = u32[12] usize;
lp_end = u32[16] usize;
magic = u8[20];
}
@ -24,7 +24,7 @@ pub fn read_sound(b: &[u8]) -> ResultS<Sound16>
0xFF => {
read_data! {
63, BE in b =>
len = u32[22] as usize;
len = u32[22] usize;
bps = u16[48];
}
@ -55,8 +55,8 @@ pub fn read_sound_def(b: &[u8]) -> ResultS<Option<(Vec<usize>, u16, SoundDef)>>
chance = u16[6];
pitch_lo = Fixed[8];
pitch_hi = Fixed[12];
n_sounds = u16[16] as usize;
grp_ofs = u32[20] as usize;
n_sounds = u16[16] usize;
grp_ofs = u32[20] usize;
}
let flags = flag_ok!(SoundFlags, flags)?;
@ -74,7 +74,7 @@ pub fn read_sound_def(b: &[u8]) -> ResultS<Option<(Vec<usize>, u16, SoundDef)>>
let mut p = 36;
for _ in 0..n_sounds {
ofs.push(grp_ofs + u32b(&b[p..]) as usize);
ofs.push(grp_ofs + usize_from_u32(u32b(&b[p..])));
p += 4;
}
@ -91,8 +91,8 @@ pub fn read_sounds(b: &[u8]) -> ResultS<Vec<SoundTable>>
260, BE in b =>
version = u32[0];
magic = Ident[4];
src_num = u16[8] as usize;
snd_num = u16[10] as usize;
src_num = u16[8] usize;
snd_num = u16[10] usize;
}
if version != 1 || magic.0 != *b"snd2" {

View File

@ -12,8 +12,8 @@ pub fn read_group(b: &[u8], text: &[u8]) -> ResultS<Group>
flags = u16[0];
ttype = u16[2];
pdata = i16[4];
start = u16[6] as usize;
size = u16[8] as usize;
start = u16[6] usize;
size = u16[8] usize;
lines = u16[10];
}
@ -29,7 +29,7 @@ pub fn read_face(b: &[u8]) -> ResultS<Face>
{
read_data! {
6, BE in b =>
start = u16[0] as usize;
start = u16[0] usize;
face = u16[2];
color = u16[4];
}
@ -45,11 +45,11 @@ pub fn read_term(b: &[u8]) -> ResultS<(Terminal, usize)>
read_data! {
10, BE in b =>
end = u16[0] as usize;
end = u16[0] usize;
encoded = u16[2];
lines = u16[4];
group_n = u16[6] as usize;
face_n = u16[8] as usize;
group_n = u16[6] usize;
face_n = u16[8] usize;
}
let encoded = encoded != 0;

View File

@ -19,7 +19,7 @@ pub fn read_chunks(b: &[u8], old_dat: bool, siz_cnk: usize)
read_data! {
p + siz_cnk, BE in b =>
iden = Ident[p];
size = u32[p + 8] as usize;
size = u32[p + 8] usize;
}
let beg = p + siz_cnk;
@ -69,8 +69,8 @@ pub fn read_entries(b: &[u8],
{
read_data! {
128, BE in b =>
dirofs = u32[72] as usize;
numents = u16[76] as usize;
dirofs = u32[72] usize;
numents = u16[76] usize;
}
let mut entries = BTreeMap::new();
@ -79,8 +79,8 @@ pub fn read_entries(b: &[u8],
for i in 0..numents {
read_data! {
p + siz_ent, BE in b =>
offset = u32[p] as usize;
size = u32[p + 4] as usize;
offset = u32[p] usize;
size = u32[p + 4] usize;
index = u16[p + 8];
}
@ -104,10 +104,10 @@ pub fn read_wad(b: &[u8]) -> ResultS<Wad>
128, BE in b =>
ver_wad = u16[0];
ver_dat = u16[2];
name = mac_roman_conv[4..68] nt;
siz_app = u16[78] as usize;
siz_wcnk = u16[80] as usize;
siz_went = u16[82] as usize;
name = mac_roman_conv[4..68] no_try;
siz_app = u16[78] usize;
siz_wcnk = u16[80] usize;
siz_went = u16[82] usize;
}
let ver_wad = Ver::from_repr(ver_wad)?;