Skip to content

Commit

Permalink
Use impl Into<Stroke> as argument in a few more places (#3420)
Browse files Browse the repository at this point in the history
* Functions that take Stroke were updated to take Into<Stroke> to make
them consistent with other Into<Stroke> parameters.
* Vec2 implements DivAssign<f32>, to make it consistent with already
implementing MulAssign<f32> and Div<f32>.
* Vec2::angled() uses sin_cos() rather than an individual sin() and
cos() call for an immeasurable but hypothetical performance improvement.
* Disable the lock_reentry_single_thread() mutex test. Lock()ing twice
on the same thread is not guaranteed to panic.

* Closes <#3419>.
  • Loading branch information
Phen-Ro authored Nov 10, 2023
1 parent 5201c04 commit 5f4046d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/egui/src/containers/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl Frame {
}

#[inline]
pub fn stroke(mut self, stroke: Stroke) -> Self {
self.stroke = stroke;
pub fn stroke(mut self, stroke: impl Into<Stroke>) -> Self {
self.stroke = stroke.into();
self
}

Expand Down
8 changes: 7 additions & 1 deletion crates/egui/src/containers/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,16 @@ pub fn paint_resize_corner(ui: &Ui, response: &Response) {
paint_resize_corner_with_style(ui, &response.rect, stroke, Align2::RIGHT_BOTTOM);
}

pub fn paint_resize_corner_with_style(ui: &Ui, rect: &Rect, stroke: Stroke, corner: Align2) {
pub fn paint_resize_corner_with_style(
ui: &Ui,
rect: &Rect,
stroke: impl Into<Stroke>,
corner: Align2,
) {
let painter = ui.painter();
let cp = painter.round_pos_to_pixels(corner.pos_in_rect(rect));
let mut w = 2.0;
let stroke = stroke.into();

while w <= rect.width() && w <= rect.height() {
painter.line_segment(
Expand Down
7 changes: 6 additions & 1 deletion crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,12 @@ impl<'open> Window<'open> {
}
}

fn paint_resize_corner(ui: &Ui, possible: &PossibleInteractions, outer_rect: Rect, stroke: Stroke) {
fn paint_resize_corner(
ui: &Ui,
possible: &PossibleInteractions,
outer_rect: Rect,
stroke: impl Into<Stroke>,
) {
let corner = if possible.resize_right && possible.resize_bottom {
Align2::RIGHT_BOTTOM
} else if possible.resize_left && possible.resize_bottom {
Expand Down
3 changes: 2 additions & 1 deletion crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,13 @@ impl Painter {
}

/// Show an arrow starting at `origin` and going in the direction of `vec`, with the length `vec.length()`.
pub fn arrow(&self, origin: Pos2, vec: Vec2, stroke: Stroke) {
pub fn arrow(&self, origin: Pos2, vec: Vec2, stroke: impl Into<Stroke>) {
use crate::emath::*;
let rot = Rot2::from_angle(std::f32::consts::TAU / 10.0);
let tip_length = vec.length() / 4.0;
let tip = origin + vec;
let dir = vec.normalized();
let stroke = stroke.into();
self.line_segment([origin, tip], stroke);
self.line_segment([tip, tip - tip_length * (rot * dir)], stroke);
self.line_segment([tip, tip - tip_length * (rot.inverse() * dir)], stroke);
Expand Down
29 changes: 27 additions & 2 deletions crates/emath/src/vec2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// A vector has a direction and length.
/// A [`Vec2`] is often used to represent a size.
Expand Down Expand Up @@ -203,7 +203,8 @@ impl Vec2 {
/// ```
#[inline(always)]
pub fn angled(angle: f32) -> Self {
vec2(angle.cos(), angle.sin())
let (sin, cos) = angle.sin_cos();
vec2(cos, sin)
}

#[must_use]
Expand Down Expand Up @@ -407,6 +408,14 @@ impl MulAssign<f32> for Vec2 {
}
}

impl DivAssign<f32> for Vec2 {
#[inline(always)]
fn div_assign(&mut self, rhs: f32) {
self.x /= rhs;
self.y /= rhs;
}
}

impl Mul<f32> for Vec2 {
type Output = Vec2;

Expand Down Expand Up @@ -470,4 +479,20 @@ fn test_vec2() {
assert_eq!(Vec2::DOWN.angle(), 0.25 * TAU);
almost_eq!(Vec2::LEFT.angle(), 0.50 * TAU);
assert_eq!(Vec2::UP.angle(), -0.25 * TAU);

let mut assignment = vec2(1.0, 2.0);
assignment += vec2(3.0, 4.0);
assert_eq!(assignment, vec2(4.0, 6.0));

let mut assignment = vec2(4.0, 6.0);
assignment -= vec2(1.0, 2.0);
assert_eq!(assignment, vec2(3.0, 4.0));

let mut assignment = vec2(1.0, 2.0);
assignment *= 2.0;
assert_eq!(assignment, vec2(2.0, 4.0));

let mut assignment = vec2(2.0, 4.0);
assignment /= 2.0;
assert_eq!(assignment, vec2(1.0, 2.0));
}
8 changes: 0 additions & 8 deletions crates/epaint/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,6 @@ mod tests {
let _b = two.lock();
}

#[test]
#[should_panic]
fn lock_reentry_single_thread() {
let one = Mutex::new(());
let _a = one.lock();
let _a2 = one.lock(); // panics
}

#[test]
fn lock_multiple_threads() {
use std::sync::Arc;
Expand Down

0 comments on commit 5f4046d

Please sign in to comment.