Skip to content

Commit

Permalink
derive deref
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamby777 committed Mar 9, 2024
1 parent 23128b7 commit 656b359
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 23 deletions.
1 change: 1 addition & 0 deletions pets-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
concat-idents = "1.1.5"
derived-deref = "2.1.0"
dialogical = "*"
godot = { git = "https://github.com/godot-rust/gdext", branch = "master", features = ["experimental-threads"]}
indoc = "2.0.4"
Expand Down
2 changes: 1 addition & 1 deletion pets-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod prelude {

// is this bad practice? no clue and idc honestly
// it's convenient with no real caveat, therefore...
pub use derived_deref::{Deref, DerefMut};
pub use indoc::indoc;
pub use rand::Rng;
pub use ribbons::unwrap_fmt;
Expand All @@ -62,7 +63,6 @@ mod prelude {
pub use std::collections::{HashMap, HashSet};
pub use std::fmt::{self, Debug, Display};
pub use std::io;
pub use std::ops::{Deref, DerefMut};
pub use std::path::{Path, PathBuf};
pub use std::rc::Rc;
}
Expand Down
14 changes: 5 additions & 9 deletions pets-lib/src/limiq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
//! Limited Queue stuff
//!
use crate::prelude::*;
use std::collections::VecDeque;
use std::ops::{Deref, Index};

/// "Limited Queue"
/// Circular buffer that pops from the back if over capacity
/// Technically, yes, it's a deque. However, this is you: 🤓
#[derive(Deref)]
pub struct LimiQ<T> {
#[target]
buffer: VecDeque<T>,

capacity: usize,
}

Expand Down Expand Up @@ -37,16 +40,9 @@ impl<T> LimiQ<T> {
}
}

impl<T> Index<usize> for LimiQ<T> {
impl<T> std::ops::Index<usize> for LimiQ<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
self.buffer.index(index)
}
}

impl<T> Deref for LimiQ<T> {
type Target = VecDeque<T>;
fn deref(&self) -> &Self::Target {
&self.buffer
}
}
16 changes: 3 additions & 13 deletions pets-lib/src/wrapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ pub fn process_input<T: Clone>(wrap: &mut Wrapped<T>, dir: ListDirection) -> Lis
Walk(old, new)
}

#[derive(Deref, DerefMut)]
/// Wrapping vector with a selected index
pub struct Wrapped<T> {
#[target]
elements: Vec<T>,

selected: Option<usize>,
}

Expand Down Expand Up @@ -126,19 +129,6 @@ where
Wrapped::new(children)
}

impl<T> Deref for Wrapped<T> {
type Target = Vec<T>;
fn deref(&self) -> &Self::Target {
&self.elements
}
}

impl<T> DerefMut for Wrapped<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.elements
}
}

// ////////////////////////////////////////////////////////////// //
// ignore the boilerplate crap below, the compiler was being dumb //
// ////////////////////////////////////////////////////////////// //
Expand Down

0 comments on commit 656b359

Please sign in to comment.