Skip to content

Commit

Permalink
refactor: remove some useless use of cast
Browse files Browse the repository at this point in the history
prefer use from implementation of std.
Ex: cast::usize(xx) -> usize::from(xx)
  • Loading branch information
gwen-lg committed Jul 18, 2024
1 parent eaca452 commit 2921a3c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/content/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl Area {
#[must_use]
pub fn size(&self) -> Size {
Size {
w: cast::usize(self.width()),
h: cast::usize(self.height()),
w: usize::from(self.width()),
h: usize::from(self.height()),
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/vobsub/img.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Run-length encoded image format for subtitles.
use cast;
use log::trace;
use nom::{
bits::complete::{tag as tag_bits, take as take_bits},
Expand Down Expand Up @@ -82,7 +81,7 @@ fn scan_line(input: &[u8], output: &mut [u8]) -> Result<usize, Error> {
let count = if run.cnt == 0 {
width - x
} else {
cast::usize(run.cnt)
usize::from(run.cnt)
};
if x + count > output.len() {
return Err(Error::ToSmallOutput {
Expand Down
17 changes: 8 additions & 9 deletions src/vobsub/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//!
//! [subs]: http://sam.zoy.org/writings/dvd/subtitles/
use cast;
use image::{ImageBuffer, Rgba, RgbaImage};
use log::{trace, warn};
use nom::{
Expand Down Expand Up @@ -242,15 +241,15 @@ impl Subtitle {
/// Decompress to subtitle to an RBGA image.
#[must_use]
pub fn to_image(&self, palette: &Palette) -> RgbaImage {
let width = cast::u32(self.area.width());
let height = cast::u32(self.area.height());
let width = u32::from(self.area.width());
let height = u32::from(self.area.height());
ImageBuffer::from_fn(width, height, |x, y| {
let offset = cast::usize(y * width + x);
// We need to subtract the raw index from 3 to get the same
// results as everybody else. I found this by inspecting the
// Handbrake subtitle decoding routines.
let px = cast::usize(3 - self.raw_image[offset]);
let rgb = palette[cast::usize(self.palette[px])].0;
let px = usize::from(3 - self.raw_image[offset]);
let rgb = palette[usize::from(self.palette[px])].0;
let a = self.alpha[px];
let aa = a << 4 | a;
Rgba([rgb[0], rgb[1], rgb[2], aa])
Expand Down Expand Up @@ -377,7 +376,7 @@ fn subtitle(raw_data: &[u8], base_time: f64) -> Result<Subtitle, VobSubError> {

// Figure out where to look for the next control sequence,
// if any.
let next_control_offset = cast::usize(control.next);
let next_control_offset = usize::from(control.next);
match control_offset.cmp(&next_control_offset) {
Ordering::Greater => {
return Err(VobSubError::ControlOffsetWentBackwards);
Expand Down Expand Up @@ -412,9 +411,9 @@ fn subtitle(raw_data: &[u8], base_time: f64) -> Result<Subtitle, VobSubError> {
// actually want to remove `end` entirely here and allow the scan lines
// to go to the end of the packet, but I've never seen that in
// practice.)
let start_0 = cast::usize(rle_offsets[0]);
let start_1 = cast::usize(rle_offsets[1]);
let end = cast::usize(initial_control_offset + 2);
let start_0 = usize::from(rle_offsets[0]);
let start_1 = usize::from(rle_offsets[1]);
let end = initial_control_offset + 2;
if start_0 > start_1 || start_1 > end {
return Err(VobSubError::InvalidScanLineOffsets {
start_0,
Expand Down

0 comments on commit 2921a3c

Please sign in to comment.