Skip to content

Commit

Permalink
chore: Use into() in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
relrelb authored and Herschel committed Jun 22, 2021
1 parent b44f3f2 commit 0fd1c05
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 108 deletions.
10 changes: 5 additions & 5 deletions core/src/avm1/activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
fn action_ascii_to_char(&mut self) -> Result<FrameControl<'gc>, Error<'gc>> {
// In SWF6+, this operates on UTF-16 code units.
// TODO: In SWF5 and below, this operates on bytes, regardless of the locale encoding.
let char_code = u32::from(self.context.avm1.pop().coerce_to_u16(self)?);
let char_code: u32 = self.context.avm1.pop().coerce_to_u16(self)?.into();
let result = if char_code != 0 {
// Unpaired surrogates turn into replacement char.
char::try_from(char_code)
Expand Down Expand Up @@ -785,7 +785,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
};

if let Some((clip, frame)) = call_frame {
if frame <= u32::from(u16::MAX) {
if frame <= u16::MAX.into() {
for action in clip.actions_on_frame(&mut self.context, frame as u16) {
let _ = self.run_child_frame_for_action(
"[Frame Call]",
Expand Down Expand Up @@ -1466,7 +1466,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {

fn action_init_array(&mut self) -> Result<FrameControl<'gc>, Error<'gc>> {
let num_elements = self.context.avm1.pop().coerce_to_f64(self)?;
let result = if num_elements < 0.0 || num_elements > i32::MAX as f64 {
let result = if num_elements < 0.0 || num_elements > i32::MAX.into() {
// InitArray pops no args and pushes undefined if num_elements is out of range.
Value::Undefined
} else {
Expand All @@ -1484,7 +1484,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {

fn action_init_object(&mut self) -> Result<FrameControl<'gc>, Error<'gc>> {
let num_props = self.context.avm1.pop().coerce_to_f64(self)?;
let result = if num_props < 0.0 || num_props > i32::MAX as f64 {
let result = if num_props < 0.0 || num_props > i32::MAX.into() {
// InitArray pops no args and pushes undefined if num_props is out of range.
Value::Undefined
} else {
Expand Down Expand Up @@ -1594,7 +1594,7 @@ impl<'a, 'gc, 'gc_context> Activation<'a, 'gc, 'gc_context> {
fn action_mb_ascii_to_char(&mut self) -> Result<FrameControl<'gc>, Error<'gc>> {
// In SWF6+, this operates on UTF-16 code units.
// TODO: In SWF5 and below, this operates on locale-dependent characters.
let char_code = u32::from(self.context.avm1.pop().coerce_to_u16(self)?);
let char_code: u32 = self.context.avm1.pop().coerce_to_u16(self)?.into();
let result = if char_code != 0 {
// Unpaired surrogates turn into replacement char.
char::try_from(char_code)
Expand Down
12 changes: 2 additions & 10 deletions core/src/backend/audio/decoders/adpcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,7 @@ impl<R: Read> AdpcmDecoder<R> {
} else {
self.left_sample += delta;
}
if self.left_sample < (i16::MIN as i32) {
self.left_sample = i16::MIN as i32;
} else if self.left_sample > (i16::MAX as i32) {
self.left_sample = i16::MAX as i32;
}
self.left_sample = (self.left_sample as i16).into();

let i = magnitude as usize;
self.left_step_index += Self::INDEX_TABLE[self.bits_per_sample - 2][i];
Expand All @@ -176,11 +172,7 @@ impl<R: Read> AdpcmDecoder<R> {
} else {
self.right_sample += delta;
}
if self.right_sample < (i16::MIN as i32) {
self.right_sample = i16::MIN as i32;
} else if self.right_sample > (i16::MAX as i32) {
self.right_sample = i16::MAX as i32;
}
self.right_sample = (self.right_sample as i16).into();

let i = magnitude as usize;
self.right_step_index += Self::INDEX_TABLE[self.bits_per_sample - 2][i];
Expand Down
16 changes: 8 additions & 8 deletions core/src/display_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,14 +1725,14 @@ impl SoundTransform {
const MAX_VOLUME: i64 = SoundTransform::MAX_VOLUME as i64;
self.volume = (i64::from(self.volume) * i64::from(other.volume) / MAX_VOLUME) as i32;

let ll0 = i64::from(self.left_to_left);
let lr0 = i64::from(self.left_to_right);
let rl0 = i64::from(self.right_to_left);
let rr0 = i64::from(self.right_to_right);
let ll1 = i64::from(other.left_to_left);
let lr1 = i64::from(other.left_to_right);
let rl1 = i64::from(other.right_to_left);
let rr1 = i64::from(other.right_to_right);
let ll0: i64 = self.left_to_left.into();
let lr0: i64 = self.left_to_right.into();
let rl0: i64 = self.right_to_left.into();
let rr0: i64 = self.right_to_right.into();
let ll1: i64 = other.left_to_left.into();
let lr1: i64 = other.left_to_right.into();
let rl1: i64 = other.right_to_left.into();
let rr1: i64 = other.right_to_right.into();
self.left_to_left = ((ll0 * ll1 + rl0 * lr1) / MAX_VOLUME) as i32;
self.left_to_right = ((lr0 * ll1 + rr0 * lr1) / MAX_VOLUME) as i32;
self.right_to_left = ((ll0 * rl1 + rl0 * rr1) / MAX_VOLUME) as i32;
Expand Down
6 changes: 3 additions & 3 deletions core/src/display_object/movie_clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ impl<'gc> MovieClip<'gc> {
} else {
reader.read_remove_object_2()
}?;
let depth = Depth::from(remove_object.depth);
let depth: Depth = remove_object.depth.into();
if let Some(i) = goto_commands.iter().position(|o| o.depth() == depth) {
goto_commands.swap_remove(i);
}
Expand Down Expand Up @@ -1912,7 +1912,7 @@ impl<'gc> TDisplayObject<'gc> for MovieClip<'gc> {
require_button_mode: bool,
) -> Option<DisplayObject<'gc>> {
if self.visible() {
let this = DisplayObject::from(*self);
let this: DisplayObject<'gc> = (*self).into();

if let Some(masker) = self.masker() {
if !masker.hit_test_shape(context, point, HitTestOptions::SKIP_INVISIBLE) {
Expand Down Expand Up @@ -2224,7 +2224,7 @@ impl<'gc> MovieClipData<'gc> {
}?;

// We merge the deltas from this PlaceObject with the previous command.
let depth = Depth::from(place_object.depth);
let depth: Depth = place_object.depth.into();
let mut goto_place =
GotoPlaceObject::new(self.current_frame(), place_object, is_rewind, index);
if let Some(i) = goto_commands.iter().position(|o| o.depth() == depth) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/display_object/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'gc> TDisplayObject<'gc> for Text<'gc> {
let mut matrix = glyph_matrix;
matrix.invert();
let point = matrix * point;
let glyph_bounds = BoundingBox::from(&glyph.shape.shape_bounds);
let glyph_bounds: BoundingBox = (&glyph.shape.shape_bounds).into();
if glyph_bounds.contains(point)
&& crate::shape_utils::shape_hit_test(
&glyph.shape,
Expand Down
2 changes: 1 addition & 1 deletion core/src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Drawing {
cursor: (Twips::ZERO, Twips::ZERO),
};

let shape = DistilledShape::from(shape);
let shape: DistilledShape = shape.into();
for path in shape.paths {
match path {
DrawPath::Stroke {
Expand Down
8 changes: 4 additions & 4 deletions core/src/html/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ where
let height = self.extent_y - self.offset_y;

(
Position::from((self.offset_x, self.offset_y)),
Size::from((width, height)),
(self.offset_x, self.offset_y).into(),
(width, height).into(),
)
}
}
Expand All @@ -231,11 +231,11 @@ where
}

pub fn origin(&self) -> Position<T> {
Position::from((self.offset_x(), self.offset_y()))
(self.offset_x(), self.offset_y()).into()
}

pub fn extent(&self) -> Position<T> {
Position::from((self.extent_x(), self.extent_y()))
(self.extent_x(), self.extent_y()).into()
}
}

Expand Down
8 changes: 3 additions & 5 deletions core/src/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,9 @@ impl<'a, 'gc> LayoutContext<'a, 'gc> {

//Flash ignores trailing spaces when aligning lines, so should we
if self.current_line_span.align != swf::TextAlign::Left {
linebox.bounds = linebox.bounds.with_size(Size::from(font.measure(
text.trim_end(),
params,
false,
)));
linebox.bounds = linebox
.bounds
.with_size(font.measure(text.trim_end(), params, false).into());
}

if let Some(line_bounds) = &mut line_bounds {
Expand Down
42 changes: 21 additions & 21 deletions core/src/html/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn position_add() {
let pos1 = Position::from((12, 31));
let pos2 = Position::from((1, -160));

assert_eq!(pos1 + pos2, Position::from((13, -129)));
assert_eq!(pos1 + pos2, (13, -129).into());
}

#[test]
Expand All @@ -19,7 +19,7 @@ fn position_add_assign() {

pos1 += pos2;

assert_eq!(pos1, Position::from((13, -129)));
assert_eq!(pos1, (13, -129).into());
}

#[test]
Expand Down Expand Up @@ -54,8 +54,8 @@ fn from_swf_rectangle() {
assert_eq!(
(pos, size),
(
Position::from((Twips::new(3), Twips::new(5))),
Size::from((Twips::new(20), Twips::new(80)))
(Twips::new(3), Twips::new(5)).into(),
(Twips::new(20), Twips::new(80)).into(),
)
)
}
Expand All @@ -67,7 +67,7 @@ fn bounds_extents() {
let bounds = BoxBounds::from_position_and_size(pos, size);
let extent_pos = Position::from((bounds.extent_x(), bounds.extent_y()));

assert_eq!(extent_pos, Position::from((Twips::new(23), Twips::new(85))));
assert_eq!(extent_pos, (Twips::new(23), Twips::new(85)).into());
}

#[test]
Expand All @@ -85,8 +85,8 @@ fn bounds_union() {
assert_eq!(
union.into_position_and_size(),
(
Position::from((Twips::new(3), Twips::new(5))),
Size::from((Twips::new(57), Twips::new(80)))
(Twips::new(3), Twips::new(5)).into(),
(Twips::new(57), Twips::new(80)).into(),
)
);
}
Expand All @@ -106,8 +106,8 @@ fn bounds_unionassign() {
assert_eq!(
bounds1.into_position_and_size(),
(
Position::from((Twips::new(3), Twips::new(5))),
Size::from((Twips::new(57), Twips::new(80)))
(Twips::new(3), Twips::new(5)).into(),
(Twips::new(57), Twips::new(80)).into(),
)
);
}
Expand All @@ -127,8 +127,8 @@ fn bounds_union_reverse() {
assert_eq!(
union.into_position_and_size(),
(
Position::from((Twips::new(3), Twips::new(5))),
Size::from((Twips::new(57), Twips::new(80)))
(Twips::new(3), Twips::new(5)).into(),
(Twips::new(57), Twips::new(80)).into(),
)
);
}
Expand All @@ -148,8 +148,8 @@ fn bounds_unionassign_reverse() {
assert_eq!(
bounds2.into_position_and_size(),
(
Position::from((Twips::new(3), Twips::new(5))),
Size::from((Twips::new(57), Twips::new(80)))
(Twips::new(3), Twips::new(5)).into(),
(Twips::new(57), Twips::new(80)).into(),
)
);
}
Expand All @@ -167,8 +167,8 @@ fn bounds_position_add() {
assert_eq!(
bounds2.into_position_and_size(),
(
Position::from((Twips::new(1), Twips::new(25))),
Size::from((Twips::new(20), Twips::new(80)))
(Twips::new(1), Twips::new(25)).into(),
(Twips::new(20), Twips::new(80)).into(),
)
);
}
Expand All @@ -186,8 +186,8 @@ fn bounds_position_addassign() {
assert_eq!(
bounds1.into_position_and_size(),
(
Position::from((Twips::new(1), Twips::new(25))),
Size::from((Twips::new(20), Twips::new(80)))
(Twips::new(1), Twips::new(25)).into(),
(Twips::new(20), Twips::new(80)).into(),
)
);
}
Expand All @@ -205,8 +205,8 @@ fn bounds_size_add() {
assert_eq!(
bounds2.into_position_and_size(),
(
Position::from((Twips::new(3), Twips::new(5))),
Size::from((Twips::new(18), Twips::new(100)))
(Twips::new(3), Twips::new(5)).into(),
(Twips::new(18), Twips::new(100)).into(),
)
);
}
Expand All @@ -224,8 +224,8 @@ fn bounds_size_addassign() {
assert_eq!(
bounds1.into_position_and_size(),
(
Position::from((Twips::new(3), Twips::new(5))),
Size::from((Twips::new(18), Twips::new(100)))
(Twips::new(3), Twips::new(5)).into(),
(Twips::new(18), Twips::new(100)).into(),
)
);
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/shape_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ pub fn shape_hit_test(
let mut has_fill_style0: bool = false;
let mut has_fill_style1: bool = false;

let min_width = f64::from(stroke_minimum_width(local_matrix));
let min_width = stroke_minimum_width(local_matrix);
let mut stroke_width = None;
let mut line_styles = &shape.styles.line_styles;

Expand Down Expand Up @@ -894,7 +894,7 @@ pub fn draw_command_stroke_hit_test(
(point_x, point_y): (Twips, Twips),
local_matrix: &Matrix,
) -> bool {
let stroke_min_width = f64::from(stroke_minimum_width(local_matrix));
let stroke_min_width = stroke_minimum_width(local_matrix);
let stroke_width = 0.5 * f64::max(stroke_width.get().into(), stroke_min_width);
let stroke_widths = (stroke_width, stroke_width * stroke_width);
let mut x = Twips::default();
Expand Down Expand Up @@ -935,10 +935,10 @@ pub fn draw_command_stroke_hit_test(
/// TODO: Verify the actual behavior; I think it's more like the average between scaleX and scaleY.
/// Does not yet support vertical/horizontal stroke scaling flags.
/// This might be better to add as a method to Matrix.
fn stroke_minimum_width(matrix: &Matrix) -> f32 {
fn stroke_minimum_width(matrix: &Matrix) -> f64 {
let sx = (matrix.a * matrix.a + matrix.b * matrix.b).sqrt();
let sy = (matrix.c * matrix.c + matrix.d * matrix.d).sqrt();
let scale = sx.max(sy);
let scale: f64 = sx.max(sy).into();
20.0 * scale
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub fn swf_char_to_lowercase(c: char) -> char {
if c.is_ascii() {
return c.to_ascii_lowercase();
}
let code_pt = u32::from(c);
if code_pt < 65536 {
let code_pt: u32 = c.into();
if code_pt <= u16::MAX.into() {
let code_pt = code_pt as u16;
match LOWERCASE_TABLE.binary_search_by(|&(key, _)| key.cmp(&code_pt)) {
Ok(i) => unsafe { std::char::from_u32_unchecked(LOWERCASE_TABLE[i].1.into()) },
Expand All @@ -67,8 +67,8 @@ pub fn swf_char_to_uppercase(c: char) -> char {
if c.is_ascii() {
return c.to_ascii_uppercase();
}
let code_pt = u32::from(c);
if code_pt < 65536 {
let code_pt: u32 = c.into();
if code_pt <= u16::MAX.into() {
let code_pt = code_pt as u16;
match UPPERCASE_TABLE.binary_search_by(|&(key, _)| key.cmp(&code_pt)) {
Ok(i) => unsafe { std::char::from_u32_unchecked(UPPERCASE_TABLE[i].1.into()) },
Expand Down
2 changes: 1 addition & 1 deletion desktop/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
println!("cargo:rustc-cfg=nightly");
}

let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let out_dir: PathBuf = env::var_os("OUT_DIR").unwrap().into();

File::create(out_dir.join("version-info.txt"))
.unwrap()
Expand Down
9 changes: 4 additions & 5 deletions desktop/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ impl AudioBackend for CpalAudioBackend {
fn register_sound(&mut self, swf_sound: &swf::Sound) -> Result<SoundHandle, Error> {
// Slice off latency seek for MP3 data.
let (skip_sample_frames, data) = if swf_sound.format.compression == AudioCompression::Mp3 {
let skip_sample_frames =
u16::from(swf_sound.data[0]) | (u16::from(swf_sound.data[1]) << 8);
let skip_sample_frames = u16::from_le_bytes([swf_sound.data[0], swf_sound.data[1]]);
(skip_sample_frames, &swf_sound.data[2..])
} else {
(0, swf_sound.data)
Expand Down Expand Up @@ -482,7 +481,7 @@ impl EventSoundSignal {
num_sample_frames: u32,
skip_sample_frames: u16,
) -> Self {
let skip_sample_frames = u32::from(skip_sample_frames);
let skip_sample_frames: u32 = skip_sample_frames.into();
let sample_divisor = 44100 / u32::from(decoder.sample_rate());
let start_sample_frame =
settings.in_sample.unwrap_or(0) / sample_divisor + skip_sample_frames;
Expand Down Expand Up @@ -601,8 +600,8 @@ impl dasp::signal::Signal for EnvelopeSignal {
fn next(&mut self) -> Self::Frame {
// Calculate interpolated volume.
let out = if self.prev_point.sample < self.next_point.sample {
let a = f64::from(self.cur_sample - self.prev_point.sample);
let b = f64::from(self.next_point.sample - self.prev_point.sample);
let a: f64 = (self.cur_sample - self.prev_point.sample).into();
let b: f64 = (self.next_point.sample - self.prev_point.sample).into();
let lerp = a / b;
let interpolator = dasp::interpolate::linear::Linear::new(
[self.prev_point.left_volume, self.prev_point.right_volume],
Expand Down
Loading

0 comments on commit 0fd1c05

Please sign in to comment.