esca 0.3.0

A chess model that answers what is true about a position: variants, positions, games, and move text.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//! Board vocabulary: colours, roles, pieces, squares and sets of squares.

use core::fmt;
use core::ops::{
    BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
};
use core::str::FromStr;

use cozy_chess as cc;

/// A player: White or Black.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub enum Colour {
    /// The side that moves first in the starting array.
    White,
    /// The side that moves second in the starting array.
    Black,
}

impl Colour {
    /// Both colours, White first.
    pub const ALL: [Colour; 2] = [Colour::White, Colour::Black];

    /// 0 for White, 1 for Black.
    #[inline]
    pub const fn index(self) -> usize {
        self as usize
    }

    /// The FEN side-to-move letter: `w` or `b`.
    #[inline]
    pub const fn to_char(self) -> char {
        match self {
            Colour::White => 'w',
            Colour::Black => 'b',
        }
    }

    /// The colour written `w` or `b`.
    #[inline]
    pub const fn from_char(c: char) -> Option<Colour> {
        match c {
            'w' => Some(Colour::White),
            'b' => Some(Colour::Black),
            _ => None,
        }
    }

    #[inline]
    pub(crate) const fn to_cozy(self) -> cc::Color {
        match self {
            Colour::White => cc::Color::White,
            Colour::Black => cc::Color::Black,
        }
    }

    #[inline]
    pub(crate) const fn from_cozy(c: cc::Color) -> Colour {
        match c {
            cc::Color::White => Colour::White,
            cc::Color::Black => Colour::Black,
        }
    }
}

impl Not for Colour {
    type Output = Colour;

    #[inline]
    fn not(self) -> Colour {
        match self {
            Colour::White => Colour::Black,
            Colour::Black => Colour::White,
        }
    }
}

impl fmt::Display for Colour {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Colour::White => "w",
            Colour::Black => "b",
        })
    }
}

/// What a piece is, without its colour.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub enum Role {
    /// Pawn.
    Pawn,
    /// Knight.
    Knight,
    /// Bishop.
    Bishop,
    /// Rook.
    Rook,
    /// Queen.
    Queen,
    /// King.
    King,
}

impl Role {
    /// Every role, in ascending conventional value.
    pub const ALL: [Role; 6] = [
        Role::Pawn,
        Role::Knight,
        Role::Bishop,
        Role::Rook,
        Role::Queen,
        Role::King,
    ];

    /// 0 for a pawn through 5 for a king.
    #[inline]
    pub const fn index(self) -> usize {
        self as usize
    }

    /// The lower-case FEN letter.
    #[inline]
    pub const fn to_char(self) -> char {
        match self {
            Role::Pawn => 'p',
            Role::Knight => 'n',
            Role::Bishop => 'b',
            Role::Rook => 'r',
            Role::Queen => 'q',
            Role::King => 'k',
        }
    }

    /// The upper-case letter SAN names the role by. A pawn has none.
    #[inline]
    pub const fn to_san_char(self) -> Option<char> {
        match self {
            Role::Pawn => None,
            Role::Knight => Some('N'),
            Role::Bishop => Some('B'),
            Role::Rook => Some('R'),
            Role::Queen => Some('Q'),
            Role::King => Some('K'),
        }
    }

    /// The role a FEN letter names, of either case.
    #[inline]
    pub const fn from_char(c: char) -> Option<Role> {
        match c.to_ascii_lowercase() {
            'p' => Some(Role::Pawn),
            'n' => Some(Role::Knight),
            'b' => Some(Role::Bishop),
            'r' => Some(Role::Rook),
            'q' => Some(Role::Queen),
            'k' => Some(Role::King),
            _ => None,
        }
    }

    #[inline]
    pub(crate) const fn to_cozy(self) -> cc::Piece {
        match self {
            Role::Pawn => cc::Piece::Pawn,
            Role::Knight => cc::Piece::Knight,
            Role::Bishop => cc::Piece::Bishop,
            Role::Rook => cc::Piece::Rook,
            Role::Queen => cc::Piece::Queen,
            Role::King => cc::Piece::King,
        }
    }

    #[inline]
    pub(crate) const fn from_cozy(p: cc::Piece) -> Role {
        match p {
            cc::Piece::Pawn => Role::Pawn,
            cc::Piece::Knight => Role::Knight,
            cc::Piece::Bishop => Role::Bishop,
            cc::Piece::Rook => Role::Rook,
            cc::Piece::Queen => Role::Queen,
            cc::Piece::King => Role::King,
        }
    }
}

impl fmt::Display for Role {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Role::Pawn => "p",
            Role::Knight => "n",
            Role::Bishop => "b",
            Role::Rook => "r",
            Role::Queen => "q",
            Role::King => "k",
        })
    }
}

/// A role plus a colour.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct Piece {
    /// What the piece is.
    pub role: Role,
    /// Whose it is.
    pub colour: Colour,
}

impl Piece {
    /// A piece of `role` belonging to `colour`.
    #[inline]
    pub const fn new(role: Role, colour: Colour) -> Piece {
        Piece { role, colour }
    }

    /// The FEN letter: upper case for White, lower case for Black.
    #[inline]
    pub const fn to_char(self) -> char {
        match self.colour {
            Colour::White => self.role.to_char().to_ascii_uppercase(),
            Colour::Black => self.role.to_char(),
        }
    }

    /// The piece a FEN letter names.
    #[inline]
    pub const fn from_char(c: char) -> Option<Piece> {
        let colour = if c.is_ascii_uppercase() {
            Colour::White
        } else {
            Colour::Black
        };
        match Role::from_char(c) {
            Some(role) => Some(Piece { role, colour }),
            None => None,
        }
    }
}

impl fmt::Display for Piece {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_char())
    }
}

/// A column of the board, a to h.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub enum File {
    /// File a.
    A,
    /// File b.
    B,
    /// File c.
    C,
    /// File d.
    D,
    /// File e.
    E,
    /// File f.
    F,
    /// File g.
    G,
    /// File h.
    H,
}

impl File {
    /// Every file, a to h.
    pub const ALL: [File; 8] = [
        File::A,
        File::B,
        File::C,
        File::D,
        File::E,
        File::F,
        File::G,
        File::H,
    ];

    /// 0 for a through 7 for h.
    #[inline]
    pub const fn index(self) -> usize {
        self as usize
    }

    /// The file at `index`, counting a as 0.
    #[inline]
    pub const fn from_index(index: usize) -> Option<File> {
        if index < 8 {
            Some(File::ALL[index])
        } else {
            None
        }
    }

    /// The lower-case letter.
    #[inline]
    pub const fn to_char(self) -> char {
        (b'a' + self as u8) as char
    }

    /// The file a letter of either case names.
    #[inline]
    pub const fn from_char(c: char) -> Option<File> {
        File::from_index((c.to_ascii_lowercase() as u8).wrapping_sub(b'a') as usize)
    }

    #[inline]
    pub(crate) const fn to_cozy(self) -> cc::File {
        cc::File::index_const(self as usize)
    }

    #[inline]
    pub(crate) const fn from_cozy(f: cc::File) -> File {
        File::ALL[f as usize]
    }
}

impl fmt::Display for File {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_char())
    }
}

/// A row of the board, 1 to 8.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub enum Rank {
    /// Rank 1.
    First,
    /// Rank 2.
    Second,
    /// Rank 3.
    Third,
    /// Rank 4.
    Fourth,
    /// Rank 5.
    Fifth,
    /// Rank 6.
    Sixth,
    /// Rank 7.
    Seventh,
    /// Rank 8.
    Eighth,
}

impl Rank {
    /// Every rank, 1 to 8.
    pub const ALL: [Rank; 8] = [
        Rank::First,
        Rank::Second,
        Rank::Third,
        Rank::Fourth,
        Rank::Fifth,
        Rank::Sixth,
        Rank::Seventh,
        Rank::Eighth,
    ];

    /// 0 for rank 1 through 7 for rank 8.
    #[inline]
    pub const fn index(self) -> usize {
        self as usize
    }

    /// The rank at `index`, counting rank 1 as 0.
    #[inline]
    pub const fn from_index(index: usize) -> Option<Rank> {
        if index < 8 {
            Some(Rank::ALL[index])
        } else {
            None
        }
    }

    /// The digit.
    #[inline]
    pub const fn to_char(self) -> char {
        (b'1' + self as u8) as char
    }

    /// The rank a digit names.
    #[inline]
    pub const fn from_char(c: char) -> Option<Rank> {
        Rank::from_index((c as u8).wrapping_sub(b'1') as usize)
    }

    /// The same rank counted from `colour`'s own back rank.
    #[inline]
    pub const fn relative_to(self, colour: Colour) -> Rank {
        match colour {
            Colour::White => self,
            Colour::Black => Rank::ALL[7 - self as usize],
        }
    }
}

impl fmt::Display for Rank {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_char())
    }
}

/// A set of files, one bit per file.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct FileSet(u8);

impl FileSet {
    /// No files.
    pub const EMPTY: FileSet = FileSet(0);
    /// All eight files.
    pub const FULL: FileSet = FileSet(0xFF);

    /// The set whose bit *i* is set iff file *i* is a member.
    #[inline]
    pub const fn from_bits(bits: u8) -> FileSet {
        FileSet(bits)
    }

    /// The membership bits, bit *i* for file *i*.
    #[inline]
    pub const fn bits(self) -> u8 {
        self.0
    }

    /// Whether `file` is a member.
    #[inline]
    pub const fn contains(self, file: File) -> bool {
        self.0 & (1 << file as u8) != 0
    }

    /// Adds `file`.
    #[inline]
    pub fn insert(&mut self, file: File) {
        self.0 |= 1 << file as u8;
    }

    /// Removes `file`.
    #[inline]
    pub fn remove(&mut self, file: File) {
        self.0 &= !(1 << file as u8);
    }

    /// How many files are members.
    #[inline]
    pub const fn len(self) -> u32 {
        self.0.count_ones()
    }

    /// Whether there are no members.
    #[inline]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }

    /// Whether every member of `self` is a member of `other`.
    #[inline]
    pub const fn is_subset(self, other: FileSet) -> bool {
        self.0 & !other.0 == 0
    }
}

impl fmt::Debug for FileSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("FileSet(")?;
        for file in *self {
            write!(f, "{file}")?;
        }
        f.write_str(")")
    }
}

impl BitAnd for FileSet {
    type Output = FileSet;

    #[inline]
    fn bitand(self, rhs: FileSet) -> FileSet {
        FileSet(self.0 & rhs.0)
    }
}

impl BitOr for FileSet {
    type Output = FileSet;

    #[inline]
    fn bitor(self, rhs: FileSet) -> FileSet {
        FileSet(self.0 | rhs.0)
    }
}

impl Sub for FileSet {
    type Output = FileSet;

    #[inline]
    fn sub(self, rhs: FileSet) -> FileSet {
        FileSet(self.0 & !rhs.0)
    }
}

impl Not for FileSet {
    type Output = FileSet;

    #[inline]
    fn not(self) -> FileSet {
        FileSet(!self.0)
    }
}

impl BitOrAssign for FileSet {
    #[inline]
    fn bitor_assign(&mut self, rhs: FileSet) {
        self.0 |= rhs.0;
    }
}

impl FromIterator<File> for FileSet {
    fn from_iter<I: IntoIterator<Item = File>>(iter: I) -> FileSet {
        let mut set = FileSet::EMPTY;
        for file in iter {
            set.insert(file);
        }
        set
    }
}

/// Iterator over a [`FileSet`], from file a upwards.
#[derive(Clone, Debug)]
pub struct FileSetIter(u8);

impl Iterator for FileSetIter {
    type Item = File;

    #[inline]
    fn next(&mut self) -> Option<File> {
        if self.0 == 0 {
            return None;
        }
        let index = self.0.trailing_zeros() as usize;
        self.0 &= self.0 - 1;
        File::from_index(index)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self.0.count_ones() as usize;
        (n, Some(n))
    }
}

impl ExactSizeIterator for FileSetIter {}

impl IntoIterator for FileSet {
    type Item = File;
    type IntoIter = FileSetIter;

    #[inline]
    fn into_iter(self) -> FileSetIter {
        FileSetIter(self.0)
    }
}

/// One of the 64 cells, indexed 0 to 63 with a1 = 0 and h8 = 63.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub struct Square(u8);

macro_rules! square_consts {
    ($($name:ident = $index:expr,)*) => {
        impl Square {
            $(
                #[doc = concat!("The ", stringify!($name), " square.")]
                pub const $name: Square = Square($index);
            )*

            /// Every square, a1 to h8.
            pub const ALL: [Square; 64] = [$(Square::$name),*];
        }
    };
}

square_consts! {
    A1 = 0, B1 = 1, C1 = 2, D1 = 3, E1 = 4, F1 = 5, G1 = 6, H1 = 7,
    A2 = 8, B2 = 9, C2 = 10, D2 = 11, E2 = 12, F2 = 13, G2 = 14, H2 = 15,
    A3 = 16, B3 = 17, C3 = 18, D3 = 19, E3 = 20, F3 = 21, G3 = 22, H3 = 23,
    A4 = 24, B4 = 25, C4 = 26, D4 = 27, E4 = 28, F4 = 29, G4 = 30, H4 = 31,
    A5 = 32, B5 = 33, C5 = 34, D5 = 35, E5 = 36, F5 = 37, G5 = 38, H5 = 39,
    A6 = 40, B6 = 41, C6 = 42, D6 = 43, E6 = 44, F6 = 45, G6 = 46, H6 = 47,
    A7 = 48, B7 = 49, C7 = 50, D7 = 51, E7 = 52, F7 = 53, G7 = 54, H7 = 55,
    A8 = 56, B8 = 57, C8 = 58, D8 = 59, E8 = 60, F8 = 61, G8 = 62, H8 = 63,
}

impl Square {
    /// The square where `file` and `rank` cross.
    #[inline]
    pub const fn new(file: File, rank: Rank) -> Square {
        Square(((rank as u8) << 3) | file as u8)
    }

    /// The square at `index`, a1 = 0 and h8 = 63.
    #[inline]
    pub const fn from_index(index: usize) -> Option<Square> {
        if index < 64 {
            Some(Square(index as u8))
        } else {
            None
        }
    }

    /// The index, a1 = 0 and h8 = 63.
    #[inline]
    pub const fn index(self) -> usize {
        self.0 as usize
    }

    /// The file this square is on.
    #[inline]
    pub const fn file(self) -> File {
        File::ALL[(self.0 & 7) as usize]
    }

    /// The rank this square is on.
    #[inline]
    pub const fn rank(self) -> Rank {
        Rank::ALL[(self.0 >> 3) as usize]
    }

    /// The same file, on the rank mirrored across the middle of the board.
    #[inline]
    pub const fn flip_rank(self) -> Square {
        Square::new(self.file(), Rank::ALL[7 - (self.0 >> 3) as usize])
    }

    /// Whether the square is dark. a1 is dark.
    #[inline]
    pub const fn is_dark(self) -> bool {
        (self.0 & 7) % 2 == (self.0 >> 3) % 2
    }

    /// This square alone.
    #[inline]
    pub const fn to_set(self) -> SquareSet {
        SquareSet(1u64 << self.0)
    }

    #[inline]
    pub(crate) const fn to_cozy(self) -> cc::Square {
        cc::Square::index_const(self.0 as usize)
    }

    #[inline]
    pub(crate) const fn from_cozy(s: cc::Square) -> Square {
        Square(s as u8)
    }
}

impl fmt::Display for Square {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.file(), self.rank())
    }
}

/// The value was not a square name such as `e4`.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct SquareParseError;

impl fmt::Display for SquareParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("not a square name")
    }
}

impl std::error::Error for SquareParseError {}

impl FromStr for Square {
    type Err = SquareParseError;

    fn from_str(s: &str) -> Result<Square, SquareParseError> {
        let bytes = s.as_bytes();
        if bytes.len() != 2 {
            return Err(SquareParseError);
        }
        let file = File::from_char(bytes[0] as char).ok_or(SquareParseError)?;
        let rank = Rank::from_char(bytes[1] as char).ok_or(SquareParseError)?;
        Ok(Square::new(file, rank))
    }
}

/// A set of squares, one bit per square.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct SquareSet(u64);

impl SquareSet {
    /// No squares.
    pub const EMPTY: SquareSet = SquareSet(0);
    /// All 64 squares.
    pub const FULL: SquareSet = SquareSet(!0);
    /// The 32 dark squares; a1 is dark.
    pub const DARK: SquareSet = SquareSet(0xAA55_AA55_AA55_AA55);
    /// The 32 light squares.
    pub const LIGHT: SquareSet = SquareSet(0x55AA_55AA_55AA_55AA);

    /// The set whose bit *i* is set iff square *i* is a member.
    #[inline]
    pub const fn from_bits(bits: u64) -> SquareSet {
        SquareSet(bits)
    }

    /// The membership bits, bit *i* for square *i*.
    #[inline]
    pub const fn bits(self) -> u64 {
        self.0
    }

    /// Whether `square` is a member.
    #[inline]
    pub const fn contains(self, square: Square) -> bool {
        self.0 & square.to_set().0 != 0
    }

    /// Adds `square`.
    #[inline]
    pub fn insert(&mut self, square: Square) {
        self.0 |= square.to_set().0;
    }

    /// Removes `square`.
    #[inline]
    pub fn remove(&mut self, square: Square) {
        self.0 &= !square.to_set().0;
    }

    /// How many squares are members.
    #[inline]
    pub const fn len(self) -> u32 {
        self.0.count_ones()
    }

    /// Whether there are no members.
    #[inline]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }

    /// Whether every member of `self` is a member of `other`.
    #[inline]
    pub const fn is_subset(self, other: SquareSet) -> bool {
        self.0 & !other.0 == 0
    }

    /// The lowest-indexed member.
    #[inline]
    pub const fn first(self) -> Option<Square> {
        if self.0 == 0 {
            None
        } else {
            Square::from_index(self.0.trailing_zeros() as usize)
        }
    }

    #[inline]
    pub(crate) const fn from_cozy(b: cc::BitBoard) -> SquareSet {
        SquareSet(b.0)
    }
}

impl fmt::Debug for SquareSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("SquareSet(")?;
        let mut first = true;
        for square in *self {
            if !first {
                f.write_str(" ")?;
            }
            write!(f, "{square}")?;
            first = false;
        }
        f.write_str(")")
    }
}

impl BitAnd for SquareSet {
    type Output = SquareSet;

    #[inline]
    fn bitand(self, rhs: SquareSet) -> SquareSet {
        SquareSet(self.0 & rhs.0)
    }
}

impl BitOr for SquareSet {
    type Output = SquareSet;

    #[inline]
    fn bitor(self, rhs: SquareSet) -> SquareSet {
        SquareSet(self.0 | rhs.0)
    }
}

impl BitXor for SquareSet {
    type Output = SquareSet;

    #[inline]
    fn bitxor(self, rhs: SquareSet) -> SquareSet {
        SquareSet(self.0 ^ rhs.0)
    }
}

impl Sub for SquareSet {
    type Output = SquareSet;

    #[inline]
    fn sub(self, rhs: SquareSet) -> SquareSet {
        SquareSet(self.0 & !rhs.0)
    }
}

impl Not for SquareSet {
    type Output = SquareSet;

    #[inline]
    fn not(self) -> SquareSet {
        SquareSet(!self.0)
    }
}

impl BitAndAssign for SquareSet {
    #[inline]
    fn bitand_assign(&mut self, rhs: SquareSet) {
        self.0 &= rhs.0;
    }
}

impl BitOrAssign for SquareSet {
    #[inline]
    fn bitor_assign(&mut self, rhs: SquareSet) {
        self.0 |= rhs.0;
    }
}

impl BitXorAssign for SquareSet {
    #[inline]
    fn bitxor_assign(&mut self, rhs: SquareSet) {
        self.0 ^= rhs.0;
    }
}

impl SubAssign for SquareSet {
    #[inline]
    fn sub_assign(&mut self, rhs: SquareSet) {
        self.0 &= !rhs.0;
    }
}

impl FromIterator<Square> for SquareSet {
    fn from_iter<I: IntoIterator<Item = Square>>(iter: I) -> SquareSet {
        let mut set = SquareSet::EMPTY;
        for square in iter {
            set.insert(square);
        }
        set
    }
}

/// Iterator over a [`SquareSet`], in ascending square index.
#[derive(Clone, Debug)]
pub struct SquareSetIter(u64);

impl Iterator for SquareSetIter {
    type Item = Square;

    #[inline]
    fn next(&mut self) -> Option<Square> {
        if self.0 == 0 {
            return None;
        }
        let index = self.0.trailing_zeros() as usize;
        self.0 &= self.0 - 1;
        Square::from_index(index)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self.0.count_ones() as usize;
        (n, Some(n))
    }
}

impl ExactSizeIterator for SquareSetIter {}

impl IntoIterator for SquareSet {
    type Item = Square;
    type IntoIter = SquareSetIter;

    #[inline]
    fn into_iter(self) -> SquareSetIter {
        SquareSetIter(self.0)
    }
}