1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Use Self everywhere (#3787)

This turns on the clippy lint
[`clippy::use_self`](https://rust-lang.github.io/rust-clippy/v0.0.212/index.html#use_self)
and fixes it everywhere.
This commit is contained in:
Emil Ernerfeldt
2024-01-08 17:41:21 +01:00
committed by GitHub
parent 12ad9e7b36
commit 401de05630
72 changed files with 590 additions and 580 deletions

View File

@@ -48,7 +48,7 @@ impl CubicBezierShape {
for (i, origin_point) in self.points.iter().enumerate() {
points[i] = transform * *origin_point;
}
CubicBezierShape {
Self {
points,
closed: self.closed,
fill: self.fill,
@@ -163,7 +163,8 @@ impl CubicBezierShape {
let q_end = q.sample(t_range.end);
let ctrl1 = from + q_start.to_vec2() * delta_t;
let ctrl2 = to - q_end.to_vec2() * delta_t;
CubicBezierShape {
Self {
points: [from, ctrl1, ctrl2, to],
closed: self.closed,
fill: self.fill,
@@ -398,7 +399,7 @@ impl QuadraticBezierShape {
fill: Color32,
stroke: impl Into<Stroke>,
) -> Self {
QuadraticBezierShape {
Self {
points,
closed,
fill,
@@ -412,7 +413,7 @@ impl QuadraticBezierShape {
for (i, origin_point) in self.points.iter().enumerate() {
points[i] = transform * *origin_point;
}
QuadraticBezierShape {
Self {
points,
closed: self.closed,
fill: self.fill,
@@ -644,7 +645,7 @@ impl FlatteningParameters {
let integral_step = integral_diff / count;
FlatteningParameters {
Self {
count,
integral_from,
integral_step,

View File

@@ -301,7 +301,7 @@ impl FontImage {
}
/// Clone a sub-region as a new image.
pub fn region(&self, [x, y]: [usize; 2], [w, h]: [usize; 2]) -> FontImage {
pub fn region(&self, [x, y]: [usize; 2], [w, h]: [usize; 2]) -> Self {
assert!(x + w <= self.width());
assert!(y + h <= self.height());
@@ -311,7 +311,7 @@ impl FontImage {
pixels.extend(&self.pixels[offset..(offset + w)]);
}
assert_eq!(pixels.len(), w * h);
FontImage {
Self {
size: [w, h],
pixels,
}

View File

@@ -105,7 +105,7 @@ impl Mesh {
}
/// Append all the indices and vertices of `other` to `self`.
pub fn append(&mut self, other: Mesh) {
pub fn append(&mut self, other: Self) {
crate::epaint_assert!(other.is_valid());
if self.is_empty() {
@@ -117,7 +117,7 @@ impl Mesh {
/// Append all the indices and vertices of `other` to `self` without
/// taking ownership.
pub fn append_ref(&mut self, other: &Mesh) {
pub fn append_ref(&mut self, other: &Self) {
crate::epaint_assert!(other.is_valid());
if self.is_empty() {

View File

@@ -66,9 +66,9 @@ fn shape_impl_send_sync() {
assert_send_sync::<Shape>();
}
impl From<Vec<Shape>> for Shape {
impl From<Vec<Self>> for Shape {
#[inline(always)]
fn from(shapes: Vec<Shape>) -> Self {
fn from(shapes: Vec<Self>) -> Self {
Self::Vec(shapes)
}
}
@@ -95,7 +95,7 @@ impl Shape {
/// A horizontal line.
pub fn hline(x: impl Into<Rangef>, y: f32, stroke: impl Into<Stroke>) -> Self {
let x = x.into();
Shape::LineSegment {
Self::LineSegment {
points: [pos2(x.min, y), pos2(x.max, y)],
stroke: stroke.into(),
}
@@ -104,7 +104,7 @@ impl Shape {
/// A vertical line.
pub fn vline(x: f32, y: impl Into<Rangef>, stroke: impl Into<Stroke>) -> Self {
let y = y.into();
Shape::LineSegment {
Self::LineSegment {
points: [pos2(x, y.min), pos2(x, y.max)],
stroke: stroke.into(),
}
@@ -182,7 +182,7 @@ impl Shape {
stroke: impl Into<Stroke>,
dash_length: f32,
gap_length: f32,
shapes: &mut Vec<Shape>,
shapes: &mut Vec<Self>,
) {
dashes_from_line(
points,
@@ -202,7 +202,7 @@ impl Shape {
dash_lengths: &[f32],
gap_lengths: &[f32],
dash_offset: f32,
shapes: &mut Vec<Shape>,
shapes: &mut Vec<Self>,
) {
dashes_from_line(
points,
@@ -309,7 +309,7 @@ impl Shape {
pub fn image(texture_id: TextureId, rect: Rect, uv: Rect, tint: Color32) -> Self {
let mut mesh = Mesh::with_texture(texture_id);
mesh.add_rect_with_uv(rect, uv, tint);
Shape::mesh(mesh)
Self::mesh(mesh)
}
/// The visual bounding rectangle (includes stroke widths)
@@ -346,9 +346,9 @@ impl Shape {
impl Shape {
#[inline(always)]
pub fn texture_id(&self) -> super::TextureId {
if let Shape::Mesh(mesh) = self {
if let Self::Mesh(mesh) = self {
mesh.texture_id
} else if let Shape::Rect(rect_shape) = self {
} else if let Self::Rect(rect_shape) = self {
rect_shape.fill_texture_id
} else {
super::TextureId::default()
@@ -358,45 +358,45 @@ impl Shape {
/// Move the shape by this many points, in-place.
pub fn translate(&mut self, delta: Vec2) {
match self {
Shape::Noop => {}
Shape::Vec(shapes) => {
Self::Noop => {}
Self::Vec(shapes) => {
for shape in shapes {
shape.translate(delta);
}
}
Shape::Circle(circle_shape) => {
Self::Circle(circle_shape) => {
circle_shape.center += delta;
}
Shape::LineSegment { points, .. } => {
Self::LineSegment { points, .. } => {
for p in points {
*p += delta;
}
}
Shape::Path(path_shape) => {
Self::Path(path_shape) => {
for p in &mut path_shape.points {
*p += delta;
}
}
Shape::Rect(rect_shape) => {
Self::Rect(rect_shape) => {
rect_shape.rect = rect_shape.rect.translate(delta);
}
Shape::Text(text_shape) => {
Self::Text(text_shape) => {
text_shape.pos += delta;
}
Shape::Mesh(mesh) => {
Self::Mesh(mesh) => {
mesh.translate(delta);
}
Shape::QuadraticBezier(bezier_shape) => {
Self::QuadraticBezier(bezier_shape) => {
bezier_shape.points[0] += delta;
bezier_shape.points[1] += delta;
bezier_shape.points[2] += delta;
}
Shape::CubicBezier(cubic_curve) => {
Self::CubicBezier(cubic_curve) => {
for p in &mut cubic_curve.points {
*p += delta;
}
}
Shape::Callback(shape) => {
Self::Callback(shape) => {
shape.rect = shape.rect.translate(delta);
}
}
@@ -484,7 +484,7 @@ impl PathShape {
/// Use [`Shape::line_segment`] instead if your line only connects two points.
#[inline]
pub fn line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
PathShape {
Self {
points,
closed: false,
fill: Default::default(),
@@ -495,7 +495,7 @@ impl PathShape {
/// A line that closes back to the start point again.
#[inline]
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
PathShape {
Self {
points,
closed: true,
fill: Default::default(),
@@ -512,7 +512,7 @@ impl PathShape {
fill: impl Into<Color32>,
stroke: impl Into<Stroke>,
) -> Self {
PathShape {
Self {
points,
closed: true,
fill: fill.into(),

View File

@@ -32,9 +32,9 @@ impl<T> From<&[T]> for AllocInfo {
}
impl std::ops::Add for AllocInfo {
type Output = AllocInfo;
type Output = Self;
fn add(self, rhs: AllocInfo) -> AllocInfo {
fn add(self, rhs: Self) -> Self {
use ElementSize::{Heterogenous, Homogeneous, Unknown};
let element_size = match (self.element_size, rhs.element_size) {
(Heterogenous, _) | (_, Heterogenous) => Heterogenous,
@@ -43,7 +43,7 @@ impl std::ops::Add for AllocInfo {
_ => Heterogenous,
};
AllocInfo {
Self {
element_size,
num_allocs: self.num_allocs + rhs.num_allocs,
num_elements: self.num_elements + rhs.num_elements,
@@ -53,7 +53,7 @@ impl std::ops::Add for AllocInfo {
}
impl std::ops::AddAssign for AllocInfo {
fn add_assign(&mut self, rhs: AllocInfo) {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}

View File

@@ -14,7 +14,7 @@ pub struct Stroke {
impl Stroke {
/// Same as [`Stroke::default`].
pub const NONE: Stroke = Stroke {
pub const NONE: Self = Self {
width: 0.0,
color: Color32::TRANSPARENT,
};
@@ -39,8 +39,8 @@ where
Color: Into<Color32>,
{
#[inline(always)]
fn from((width, color): (f32, Color)) -> Stroke {
Stroke::new(width, color)
fn from((width, color): (f32, Color)) -> Self {
Self::new(width, color)
}
}

View File

@@ -26,16 +26,16 @@ impl CCursor {
/// Two `CCursor`s are considered equal if they refer to the same character boundary,
/// even if one prefers the start of the next row.
impl PartialEq for CCursor {
fn eq(&self, other: &CCursor) -> bool {
fn eq(&self, other: &Self) -> bool {
self.index == other.index
}
}
impl std::ops::Add<usize> for CCursor {
type Output = CCursor;
type Output = Self;
fn add(self, rhs: usize) -> Self::Output {
CCursor {
Self {
index: self.index.saturating_add(rhs),
prefer_next_row: self.prefer_next_row,
}
@@ -43,10 +43,10 @@ impl std::ops::Add<usize> for CCursor {
}
impl std::ops::Sub<usize> for CCursor {
type Output = CCursor;
type Output = Self;
fn sub(self, rhs: usize) -> Self::Output {
CCursor {
Self {
index: self.index.saturating_sub(rhs),
prefer_next_row: self.prefer_next_row,
}
@@ -104,7 +104,7 @@ pub struct PCursor {
/// Two `PCursor`s are considered equal if they refer to the same character boundary,
/// even if one prefers the start of the next row.
impl PartialEq for PCursor {
fn eq(&self, other: &PCursor) -> bool {
fn eq(&self, other: &Self) -> bool {
self.paragraph == other.paragraph && self.offset == other.offset
}
}

View File

@@ -88,7 +88,7 @@ impl FontImpl {
ab_glyph_font: ab_glyph::FontArc,
scale_in_pixels: f32,
tweak: FontTweak,
) -> FontImpl {
) -> Self {
assert!(scale_in_pixels > 0.0);
assert!(pixels_per_point > 0.0);

View File

@@ -214,7 +214,7 @@ impl TexturesDelta {
self.set.is_empty() && self.free.is_empty()
}
pub fn append(&mut self, mut newer: TexturesDelta) {
pub fn append(&mut self, mut newer: Self) {
self.set.extend(newer.set);
self.free.append(&mut newer.free);
}

View File

@@ -58,7 +58,7 @@ impl<T: Float> Hash for OrderedFloat<T> {
impl<T> From<T> for OrderedFloat<T> {
#[inline]
fn from(val: T) -> Self {
OrderedFloat(val)
Self(val)
}
}
@@ -84,14 +84,14 @@ pub trait FloatOrd {
impl FloatOrd for f32 {
#[inline]
fn ord(self) -> OrderedFloat<f32> {
fn ord(self) -> OrderedFloat<Self> {
OrderedFloat(self)
}
}
impl FloatOrd for f64 {
#[inline]
fn ord(self) -> OrderedFloat<f64> {
fn ord(self) -> OrderedFloat<Self> {
OrderedFloat(self)
}
}
@@ -119,7 +119,7 @@ mod private {
impl FloatImpl for f32 {
#[inline]
fn is_nan(&self) -> bool {
f32::is_nan(*self)
Self::is_nan(*self)
}
#[inline]
@@ -131,7 +131,7 @@ mod private {
impl FloatImpl for f64 {
#[inline]
fn is_nan(&self) -> bool {
f64::is_nan(*self)
Self::is_nan(*self)
}
#[inline]