Skip to content

Use the Self type wherever possible #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/apperr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ pub enum Error {

impl Error {
pub const fn new_app(code: u32) -> Self {
Error::App(code)
Self::App(code)
}

pub const fn new_icu(code: u32) -> Self {
Error::Icu(code)
Self::Icu(code)
}

pub const fn new_sys(code: u32) -> Self {
Error::Sys(code)
Self::Sys(code)
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/arena/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum Arena {

impl Drop for Arena {
fn drop(&mut self) {
if let Arena::Delegated { delegate, borrow } = self {
if let Self::Delegated { delegate, borrow } = self {
let borrows = delegate.borrows.get();
assert_eq!(*borrow, borrows);
delegate.borrows.set(borrows - 1);
Expand All @@ -63,11 +63,11 @@ impl Arena {
Self::Owned { arena: release::Arena::empty() }
}

pub fn new(capacity: usize) -> apperr::Result<Arena> {
pub fn new(capacity: usize) -> apperr::Result<Self> {
Ok(Self::Owned { arena: release::Arena::new(capacity)? })
}

pub(super) fn delegated(delegate: &release::Arena) -> Arena {
pub(super) fn delegated(delegate: &release::Arena) -> Self {
let borrow = delegate.borrows.get() + 1;
delegate.borrows.set(borrow);
Self::Delegated { delegate: unsafe { mem::transmute(delegate) }, borrow }
Expand All @@ -76,22 +76,22 @@ impl Arena {
#[inline]
pub(super) fn delegate_target(&self) -> &release::Arena {
match *self {
Arena::Delegated { delegate, borrow } => {
Self::Delegated { delegate, borrow } => {
assert!(
borrow == delegate.borrows.get(),
"Arena already borrowed by a newer ScratchArena"
);
delegate
}
Arena::Owned { ref arena } => arena,
Self::Owned { ref arena } => arena,
}
}

#[inline]
pub(super) fn delegate_target_unchecked(&self) -> &release::Arena {
match self {
Arena::Delegated { delegate, .. } => delegate,
Arena::Owned { arena } => arena,
Self::Delegated { delegate, .. } => delegate,
Self::Owned { arena } => arena,
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/arena/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ impl Arena {
}
}

pub fn new(capacity: usize) -> apperr::Result<Arena> {
pub fn new(capacity: usize) -> apperr::Result<Self> {
let capacity = (capacity.max(1) + ALLOC_CHUNK_SIZE - 1) & !(ALLOC_CHUNK_SIZE - 1);
let base = unsafe { sys::virtual_reserve(capacity)? };

Ok(Arena {
Ok(Self {
base,
capacity,
commit: Cell::new(0),
Expand Down Expand Up @@ -177,7 +177,7 @@ impl Drop for Arena {

impl Default for Arena {
fn default() -> Self {
Arena::empty()
Self::empty()
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/arena/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ impl<'a> ArenaString<'a> {
/// Checks whether `text` contains only valid UTF-8.
/// If the entire string is valid, it returns `Ok(text)`.
/// Otherwise, it returns `Err(ArenaString)` with all invalid sequences replaced with U+FFFD.
pub fn from_utf8_lossy<'s>(
arena: &'a Arena,
text: &'s [u8],
) -> Result<&'s str, ArenaString<'a>> {
pub fn from_utf8_lossy<'s>(arena: &'a Arena, text: &'s [u8]) -> Result<&'s str, Self> {
let mut iter = text.utf8_chunks();
let Some(mut chunk) = iter.next() else {
return Ok("");
Expand Down
4 changes: 3 additions & 1 deletion src/bin/edit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ impl Drop for RestoreModes {
fn drop(&mut self) {
// Same as in the beginning but in the reverse order.
// It also includes DECSCUSR 0 to reset the cursor style and DECTCEM to show the cursor.
sys::write_stdout("\x1b[0 q\x1b[?25h\x1b]0;\x07\x1b[?1036l\x1b[?1002;1006;2004l\x1b[?1049l");
sys::write_stdout(
"\x1b[0 q\x1b[?25h\x1b]0;\x07\x1b[?1036l\x1b[?1002;1006;2004l\x1b[?1049l",
);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/bin/edit/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct FormatApperr(apperr::Error);

impl From<apperr::Error> for FormatApperr {
fn from(err: apperr::Error) -> Self {
FormatApperr(err)
Self(err)
}
}

Expand Down Expand Up @@ -68,19 +68,19 @@ impl Default for DisplayablePathBuf {

impl Clone for DisplayablePathBuf {
fn clone(&self) -> Self {
DisplayablePathBuf::new(self.value.clone())
Self::new(self.value.clone())
}
}

impl From<OsString> for DisplayablePathBuf {
fn from(s: OsString) -> DisplayablePathBuf {
DisplayablePathBuf::new(PathBuf::from(s))
fn from(s: OsString) -> Self {
Self::new(PathBuf::from(s))
}
}

impl<T: ?Sized + AsRef<OsStr>> From<&T> for DisplayablePathBuf {
fn from(s: &T) -> DisplayablePathBuf {
DisplayablePathBuf::new(PathBuf::from(s))
fn from(s: &T) -> Self {
Self::new(PathBuf::from(s))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/buffer/gap_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum BackingBuffer {
impl Drop for BackingBuffer {
fn drop(&mut self) {
unsafe {
if let BackingBuffer::VirtualMemory(ptr, reserve) = *self {
if let Self::VirtualMemory(ptr, reserve) = *self {
sys::virtual_release(ptr, reserve);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl TextBuffer {
/// Creates a new text buffer inside an [`Rc`].
/// See [`TextBuffer::new()`].
pub fn new_rc(small: bool) -> apperr::Result<RcTextBuffer> {
let buffer = TextBuffer::new(small)?;
let buffer = Self::new(small)?;
Ok(Rc::new(SemiRefCell::new(buffer)))
}

Expand Down
2 changes: 1 addition & 1 deletion src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod release {

impl<'b, T> Ref<'b, T> {
#[inline(always)]
pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> {
pub fn clone(orig: &Self) -> Self {
Ref(orig.0)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ impl WriteableDocument for PathBuf {
fn replace(&mut self, range: Range<usize>, replacement: &[u8]) {
let mut vec = mem::take(self).into_os_string().into_encoded_bytes();
vec.replace_range(range, replacement);
*self = unsafe { PathBuf::from(OsString::from_encoded_bytes_unchecked(vec)) };
*self = unsafe { Self::from(OsString::from_encoded_bytes_unchecked(vec)) };
}
}
18 changes: 9 additions & 9 deletions src/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,31 +793,31 @@ pub struct Attributes(u8);

#[allow(non_upper_case_globals)]
impl Attributes {
pub const None: Attributes = Attributes(0);
pub const Italic: Attributes = Attributes(0b1);
pub const Underlined: Attributes = Attributes(0b10);
pub const All: Attributes = Attributes(0b11);
pub const None: Self = Self(0);
pub const Italic: Self = Self(0b1);
pub const Underlined: Self = Self(0b10);
pub const All: Self = Self(0b11);

pub const fn is(self, attr: Attributes) -> bool {
pub const fn is(self, attr: Self) -> bool {
(self.0 & attr.0) == attr.0
}
}

unsafe impl MemsetSafe for Attributes {}

impl BitOr for Attributes {
type Output = Attributes;
type Output = Self;

fn bitor(self, rhs: Self) -> Self::Output {
Attributes(self.0 | rhs.0)
Self(self.0 | rhs.0)
}
}

impl BitXor for Attributes {
type Output = Attributes;
type Output = Self;

fn bitxor(self, rhs: Self) -> Self::Output {
Attributes(self.0 ^ rhs.0)
Self(self.0 ^ rhs.0)
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,19 @@ pub struct Point {
}

impl Point {
pub const MIN: Point = Point { x: CoordType::MIN, y: CoordType::MIN };
pub const MAX: Point = Point { x: CoordType::MAX, y: CoordType::MAX };
pub const MIN: Self = Self { x: CoordType::MIN, y: CoordType::MIN };
pub const MAX: Self = Self { x: CoordType::MAX, y: CoordType::MAX };
}

impl PartialOrd<Point> for Point {
impl PartialOrd<Self> for Point {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Point {
fn cmp(&self, other: &Self) -> Ordering {
match self.y.cmp(&other.y) {
Ordering::Equal => self.x.cmp(&other.x),
ord => ord,
}
self.y.cmp(&other.y).then(self.x.cmp(&other.x))
}
}

Expand Down Expand Up @@ -149,7 +146,7 @@ impl Rect {
let r = l.max(r);
let b = t.max(b);

Rect { left: l, top: t, right: r, bottom: b }
Self { left: l, top: t, right: r, bottom: b }
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl InputKey {
self.0
}

pub(crate) const fn key(&self) -> InputKey {
InputKey(self.0 & 0x00FFFFFF)
pub(crate) const fn key(&self) -> Self {
Self(self.0 & 0x00FFFFFF)
}

pub(crate) const fn modifiers(&self) -> InputKeyMod {
Expand All @@ -52,8 +52,8 @@ impl InputKey {
(self.0 & modifier.0) != 0
}

pub(crate) const fn with_modifiers(&self, modifiers: InputKeyMod) -> InputKey {
InputKey(self.0 | modifiers.0)
pub(crate) const fn with_modifiers(&self, modifiers: InputKeyMod) -> Self {
Self(self.0 | modifiers.0)
}
}

Expand All @@ -67,16 +67,16 @@ impl InputKeyMod {
Self(v)
}

pub(crate) const fn contains(&self, modifier: InputKeyMod) -> bool {
pub(crate) const fn contains(&self, modifier: Self) -> bool {
(self.0 & modifier.0) != 0
}
}

impl std::ops::BitOr<InputKeyMod> for InputKey {
type Output = InputKey;
type Output = Self;

fn bitor(self, rhs: InputKeyMod) -> InputKey {
InputKey(self.0 | rhs.0)
fn bitor(self, rhs: InputKeyMod) -> Self {
Self(self.0 | rhs.0)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/simd/memchr2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod tests {
#[test]
fn test_page_boundary() {
let page = unsafe {
const PAGE_SIZE: usize = 64 * 1024; // 64 KiB to cover many architectures.
const PAGE_SIZE: usize = 64 * 1024; // 64 KiB to cover many architectures.

// 3 pages: uncommitted, committed, uncommitted
let ptr = sys::virtual_reserve(PAGE_SIZE * 3).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/simd/memrchr2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod tests {
#[test]
fn test_page_boundary() {
let page = unsafe {
const PAGE_SIZE: usize = 64 * 1024; // 64 KiB to cover many architectures.
const PAGE_SIZE: usize = 64 * 1024; // 64 KiB to cover many architectures.

// 3 pages: uncommitted, committed, uncommitted
let ptr = sys::virtual_reserve(PAGE_SIZE * 3).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,14 @@ pub enum FileId {
impl PartialEq for FileId {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(FileId::Id(left), FileId::Id(right)) => {
(Self::Id(left), Self::Id(right)) => {
// Lowers to an efficient word-wise comparison.
const SIZE: usize = std::mem::size_of::<FileSystem::FILE_ID_INFO>();
let a: &[u8; SIZE] = unsafe { mem::transmute(left) };
let b: &[u8; SIZE] = unsafe { mem::transmute(right) };
a == b
}
(FileId::Path(left), FileId::Path(right)) => left == right,
(Self::Path(left), Self::Path(right)) => left == right,
_ => false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ impl Tui {
if ptr::eq(focused_next, focused_start) {
false
} else {
Tui::build_node_path(Some(focused_next), &mut self.focused_node_path);
Self::build_node_path(Some(focused_next), &mut self.focused_node_path);
true
}
}
Expand Down