Skip to main content

BasicPile

Struct BasicPile 

Source
pub struct BasicPile(/* private fields */);

Implementations§

Source§

impl BasicPile

Source

pub fn v(&self) -> &Vec<BasicCard>

Returns a reference to the internal vector of the struct.

Source

pub fn draw(&mut self, n: usize) -> Option<Self>

Returns n number of BasicCards from the beginning of the BasicPile. If there are not enough cards in the BasicPile to satisfy the request, None is returned.

CoPilot’s suggestion:

use card_game_engine::prelude::{BasicPile, Pile};
Where TF is CoPilot getting card_game_engine from???
use cardpack::prelude::*;

let mut pile = Pinochle::deck().into_basic_pile();
let hand = pile.draw(5).unwrap();

assert_eq!(hand.to_string(), "A♠ A♠ T♠ T♠ K♠");
Source

pub fn draw_first(&mut self) -> Option<BasicCard>

This is very much suboptimal, but I don’t have an easy way to avoid it. My common currency is vectors. The idea of treating the end of the vector as the top of the deck seems like a good one.

Source

pub fn shuffle(&mut self)

Shuffles the BasicPile in place using the process default RNG (rand::rng()). For deterministic shuffling, use shuffle_with_seed.

Source

pub fn shuffled(&self) -> Self

Returns a new shuffled version of the BasicPile.

For deterministic shuffling, use shuffled_with_seed.

Source

pub fn shuffle_with_seed(&mut self, seed: u64)

Shuffles the BasicPile in place deterministically from a u64 seed.

Uses rand::rngs::StdRng internally. Same seed produces the same permutation within one rand major version; a rand upgrade may change the result. For long-lived replay logs or cross-version reproducibility, pass a portable RNG (e.g., ChaCha8Rng from rand_chacha) to shuffle_with_rng instead.

use cardpack::prelude::*;

let pile = Pile::<Standard52>::basic_pile();
let a = pile.shuffled_with_seed(42);
let b = pile.shuffled_with_seed(42);
assert_eq!(a, b);
Source

pub fn shuffled_with_seed(&self, seed: u64) -> Self

Returns a new BasicPile shuffled deterministically from a u64 seed.

See shuffle_with_seed for the portability caveat.

Source

pub fn shuffle_with_rng<R: Rng + ?Sized>(&mut self, rng: &mut R)

Shuffles the BasicPile in place using the caller’s RNG.

Generic over any R: Rng + ?Sized. The seed-based methods are sugar over this primitive — pass your own RNG (e.g., ChaCha8Rng) for algorithm-stable reproducibility across rand major-version bumps.

Source

pub fn shuffled_with_rng<R: Rng + ?Sized>(&self, rng: &mut R) -> Self

Returns a new BasicPile shuffled using the caller’s RNG.

Source

pub fn contains(&self, card: &BasicCard) -> bool

Source

pub fn extend(&mut self, other: &Self)

Source

pub fn first(&self) -> Option<&BasicCard>

Source

pub fn get(&self, position: usize) -> Option<&BasicCard>

Source

pub fn is_empty(&self) -> bool

Source

pub fn iter(&self) -> Iter<'_, BasicCard> ⓘ

Source

pub fn len(&self) -> usize

Source

pub fn pop(&mut self) -> Option<BasicCard>

Source

pub fn push(&mut self, card: BasicCard)

Source

pub fn reverse(&mut self)

Source

pub fn remove(&mut self, position: usize) -> BasicCard

Source

pub fn sort(&mut self)

Source

pub fn sort_by_rank(&mut self)

This sorts the cards by rank instead of suit.

use cardpack::prelude::*;

let mut hand = BasicPile::from(vec![FrenchBasicCard::KING_SPADES, FrenchBasicCard::ACE_DIAMONDS]);

// By default `BasicPile` sorts by suit.
hand.sort();
assert_eq!(hand.to_string(), "K♠ A♦");

hand.sort_by_rank();
assert_eq!(hand.to_string(), "A♦ K♠");
Source

pub fn sorted(&self) -> Self

Returns a new BasicPile sorted.

use cardpack::prelude::*;

assert_eq!(basic!("2♠ A♣ 3♠ 4♠ 5♣").sorted().to_string(), "4♠ 3♠ 2♠ A♣ 5♣");
Source

pub fn sorted_by_rank(self) -> Self

Returns a new BasicPile with the BasicCards sorted.

use cardpack::prelude::*;

assert_eq!(basic!("2♠ A♣ 3♠ 4♠ 5♣").sorted_by_rank().to_string(), "A♣ 5♣ 4♠ 3♠ 2♠");

Trait Implementations§

Source§

impl Clone for BasicPile

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BasicPile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BasicPile

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for BasicPile

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for BasicPile

use cardpack::prelude::*;

let hand = BasicPile::from(
    vec![
        FrenchBasicCard::NINE_CLUBS,
        FrenchBasicCard::EIGHT_DIAMONDS,
        FrenchBasicCard::SEVEN_CLUBS,
    ]
);

assert_eq!(hand.to_string(), "9♣ 8♦ 7♣");
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for BasicPile

Source§

impl<DeckType: DeckedBase + Copy + Default + Ord + Hash> From<&Pile<DeckType>> for BasicPile

Source§

fn from(pack: &Pile<DeckType>) -> Self

Converts to this type from the input type.
Source§

impl<DeckType: DeckedBase + Default + Ord + Copy + Hash> From<BasicPile> for Pile<DeckType>

Source§

fn from(pile: BasicPile) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<BasicCard>> for BasicPile

Source§

fn from(cards: Vec<BasicCard>) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<BasicCard> for BasicPile

Source§

fn from_iter<T: IntoIterator<Item = BasicCard>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl FromIterator<BasicPile> for Combos

Source§

fn from_iter<T: IntoIterator<Item = BasicPile>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for BasicPile

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> IntoIterator for &'a BasicPile

A win for CoPilot!

I initially got an error from the Rust compiler:

warning: `iter` method without an `IntoIterator` impl for `&Pile`
   --> src/basic/types/pile.rs:163:5
    |
163 | /     pub fn iter(&self) -> std::slice::Iter<BasicCard> {
164 | |         self.0.iter()
165 | |     }
    | |_____^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_without_into_iter
note: the lint level is defined here
   --> src/lib.rs:1:9
    |
1   | #![warn(clippy::pedantic)]
    |         ^^^^^^^^^^^^^^^^
    = note: `#[warn(clippy::iter_without_into_iter)]` implied by `#[warn(clippy::pedantic)]`
help: consider implementing `IntoIterator` for `&Pile`
    |
14  +
15  + impl IntoIterator for &Pile {
16  +     type Item = &basic::types::basic_card::BasicCard;
17  +     type IntoIter = std::slice::Iter<'_, basic::types::basic_card::BasicCard>;
18  +     fn into_iter(self) -> Self::IntoIter {
19  +         self.iter()
20  +     }
21  + }
    |

Trying this code recommendation created the following error:

error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
   --> src/basic/types/pile.rs:281:17
    |
281 |     type Item = &basic::types::basic_card::BasicCard;
    |                 ^ this lifetime must come from the implemented type

error[E0637]: `'_` cannot be used here
   --> src/basic/types/pile.rs:282:38
    |
282 |     type IntoIter = std::slice::Iter<'_, basic::types::basic_card::BasicCard>;
    |                                      ^^ `'_` is a reserved lifetime name

error[E0433]: failed to resolve: use of undeclared crate or module `basic`
   --> src/basic/types/pile.rs:281:18
    |
281 |     type Item = &basic::types::basic_card::BasicCard;
    |                  ^^^^^ use of undeclared crate or module `basic`
    |
help: consider importing this module
    |
1   + use crate::basic::types::basic_card;
    |
help: if you import `basic_card`, refer to it directly
    |
281 -     type Item = &basic::types::basic_card::BasicCard;
281 +     type Item = &basic_card::BasicCard;
    |

error[E0433]: failed to resolve: use of undeclared crate or module `basic`
   --> src/basic/types/pile.rs:282:42
    |
282 |     type IntoIter = std::slice::Iter<'_, basic::types::basic_card::BasicCard>;
    |                                          ^^^^^ use of undeclared crate or module `basic`
    |
help: consider importing this module
    |
1   + use crate::basic::types::basic_card;
    |
help: if you import `basic_card`, refer to it directly
    |
282 -     type IntoIter = std::slice::Iter<'_, basic::types::basic_card::BasicCard>;
282 +     type IntoIter = std::slice::Iter<'_, basic_card::BasicCard>;
    |

This does give me hope that the mighty Rust compiler isn’t infallible. This does, however, provide one with a moment’s pause, that one has to be careful, even when getting advice from the final arbiter of truth in Rustlevania.

This led me to some DuckDuckGoing where I found an useful article by wrongbyte.

After playing around with that I came up with this:

impl<'a> IntoIterator for &Pile {
    type Item = &'a BasicCard;
    type IntoIter = std::slice::Iter<'a, BasicCard>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

which got us down to one error:

error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
   --> src/basic/types/pile.rs:326:6
    |
326 | impl<'a> IntoIterator for &Pile {
    |      ^^ unconstrained lifetime parameter

Ah yes, the dreaded error #E0207, my old friend. This is why coding in Rust is maybe not the best of ideas for idiot savants like me who had diving into this level of trivia. Maybe I’m a Zig developer in a Rust devs body or something, but I just want this shit to work.

Up till now, CoPilot has been just regurgitating back out the code I already wrote. In a final hail mary I decided to ask it and see what it came up with.

How do I fix the “the lifetime parameter 'a is not constrained by the impl trait, self type, or predicates” error on impl<’a> IntoIterator for &Pile

The error “the lifetime parameter ’a is not constrained by the impl trait, self type, or predicates” occurs because the lifetime ’a is not used in the trait implementation. To fix this, you need to specify the lifetime parameter correctly in the IntoIterator implementation.

And you can see it in action below. I was throwing lifeline’s all over the place, with no luck. I’ll be honest with you, I am grokking the logic behind their placement about as much as I was getting 11th grade Trig sitting behind Amy G. Mind you, this was the Golden age of Guess Jeans, so I am not entirely at fault here.

Here is the corrected implementation:

Source§

type Item = &'a BasicCard

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, BasicCard>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for BasicPile

Source§

type Item = BasicCard

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<BasicCard>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Ord for BasicPile

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for BasicPile

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for BasicPile

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Ranged for BasicPile

Source§

fn my_basic_pile(&self) -> BasicPile

This is the starting point for any entity enabling the Ranged trait. Since none of the logic behind any of these methods requires the boundary provided by the generic Pile struct, since it is based entirely on the raw data in the collection of BasicCards in a BasicPile. Pile is very useful for getting us to where we want to be, re a specific collection of cards, but once there we can rid ourselves of its confinements and focus on data. Read more
Source§

fn combos(&self, k: usize) -> Combos ⓘ

Source§

fn combos_with_dups(&self, k: usize) -> Combos ⓘ

Source§

fn all_of_rank(&self, rank: Pip) -> bool

Source§

fn all_of_same_rank(&self) -> bool

Source§

fn all_of_same_suit(&self) -> bool

Source§

fn is_connector(&self) -> bool

I love how CoPilot can have the earlier version of the function from Pile and completely ignore it and instead provide stuff with absolutely no rational: Read more
Source§

fn of_same_or_greater_rank(&self, rank: Pip) -> bool

Source§

fn filter_cards<F>(&self, filter: F) -> BasicPile
where F: Fn(&BasicCard) -> bool,

Source§

fn cards_of_rank_pip_type(&self, pip_type: PipType) -> BasicPile

Returns a BasicPile filtering on rank PipType. Read more
Source§

fn cards_of_suit_pip_type(&self, pip_type: PipType) -> BasicPile

Returns a BasicPile filtering on suit PipType. Read more
Source§

fn cards_with_pip_type(&self, pip_type: PipType) -> BasicPile

Returns a BasicPile where either the suit or rank Pip have the specified PipType.\ Read more
Source§

fn extract_pips<F>(&self, f: F) -> Vec<Pip>
where F: Fn(&BasicCard) -> Pip,

Source§

fn map_by_rank(&self) -> BTreeMap<Pip, BasicPile>

Source§

fn map_by_suit(&self) -> BTreeMap<Pip, BasicPile>

Source§

fn combos_by_rank(&self) -> Combos ⓘ

Takes the BasicPiles and sorts them based on their length. Read more
Source§

fn combos_by_suit(&self) -> Combos ⓘ

Source§

fn combos_from_pip_map(&self, mappy: BTreeMap<Pip, BasicPile>) -> Combos ⓘ

Source§

fn pip_index<F>(&self, f: F, joiner: &str) -> String
where F: Fn(&BasicCard) -> Pip,

Source§

fn ranks(&self) -> Vec<Pip>

Source§

fn ranks_index(&self, joiner: &str) -> String

Source§

fn ranks_by_suit(&self, suit: Pip) -> Option<Vec<Pip>>

TODO RF: Wouldn’t it be easier to just return a vector, and if it’s empty you know there were none in the Pile.
Source§

fn ranks_index_by_suit(&self, suit: Pip, joiner: &str) -> Option<String>

Source§

fn suits(&self) -> Vec<Pip>

Source§

fn suits_index(&self, joiner: &str) -> String

Source§

fn suit_symbol_index(&self, joiner: &str) -> String

Source§

impl Serialize for BasicPile

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for BasicPile

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AnyEq for T
where T: Any + PartialEq,

Source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.