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
//! Underlying representation of big integers.

use crate::{
    arch::word::{DoubleWord, Word},
    buffer::Buffer,
    primitive::{double_word, split_dword},
    Sign,
};
use core::{
    fmt::{self, Write},
    hash::{Hash, Hasher},
    mem,
    num::NonZeroIsize,
    ptr::{self, NonNull},
    slice,
};
use static_assertions::const_assert_eq;

/// This union contains the raw representation of words, the words are either inlined
/// or on the heap. The flag used to distinguishing them is the `len` field of the buffer.
#[repr(C)]
union ReprData {
    inline: [Word; 2],        // lo, hi
    heap: (*mut Word, usize), // ptr, len
}

/// Internal representation for big integers.
///
/// It's optimized so that small integers (single or double words) will not be allocated on heap.
/// When the data is allocated on the heap, it can be casted to [Buffer] efficiently, but modifying
/// the buffer inplace is not allowed because that can break the rule on the `capacity` field.
///
/// To modified the internal data, one must convert the Repr into either [TypedRepr](enum, owning the data)
/// or [Buffer](raw heap buffer). To access the internal data, one must use [TypedReprRef](enum, reference)
/// or [slice][Repr::as_slice] protocol.
#[repr(C)]
pub struct Repr {
    /// The words in the `data` field are ordered from the least significant to the most significant.
    data: ReprData,

    /// The capacity is designed to be not zero so that it provides a niche value for layout optimization.
    ///
    /// How to intepret the `data` field:
    /// - `capacity` = 1: the words are inlined and the high word is 0. (including the case where low word is also 0)
    /// - `capacity` = 2: the words are inlined
    /// - `capacity` >= 3: the words are on allocated on the heap. In this case, data.len >= 3 will also be forced.
    /// - `capacity` < 0: similiar to the cases above, but negative capacity value is used to mark the integer is negative.
    ///     Note that in this case the inlined value is not allowed to be zero. (zero must have a positive sign)
    capacity: NonZeroIsize,
}

// right now on all supported architectures, Word = usize. However, for cases where
// Word > usize, an extra padding in Buffer will be necessary for this equality to hold
const_assert_eq!(mem::size_of::<Buffer>(), mem::size_of::<Repr>());

// make sure the layout optimization is effective
const_assert_eq!(mem::size_of::<Repr>(), mem::size_of::<Option<Repr>>());

// SAFETY: the pointer to the allocated space is uniquely owned by this struct.
unsafe impl Send for Repr {}

// SAFETY: we don't provide interior mutability for Repr and Buffer
unsafe impl Sync for Repr {}

/// A strong typed safe representation of a `Repr` without sign
#[derive(Clone)]
pub enum TypedRepr {
    Small(DoubleWord),
    Large(Buffer),
}

/// A strong typed safe representation of a reference to `Repr` without sign
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum TypedReprRef<'a> {
    RefSmall(DoubleWord),
    RefLarge(&'a [Word]),
}

impl Repr {
    /// Get the length of the number (in `Word`s), return 0 when the number is zero.
    #[inline]
    pub const fn len(&self) -> usize {
        match self.capacity() {
            0 => unreachable!(),
            1 => (unsafe { self.data.inline[0] } != 0) as usize,
            2 => 2,
            _ => unsafe { self.data.heap.1 },
        }
    }

    /// Get the capacity of the representation (in `Word`s)
    ///
    /// It will not be zero even if the underlying number is zero.
    #[inline]
    pub const fn capacity(&self) -> usize {
        self.capacity.get().unsigned_abs()
    }

    /// Get the sign of the repr
    #[inline]
    pub const fn sign(&self) -> Sign {
        if self.capacity.get() > 0 {
            Sign::Positive
        } else {
            Sign::Negative
        }
    }

    /// Get the capacity of Repr and sign simultaneously
    #[inline]
    pub const fn sign_capacity(&self) -> (usize, Sign) {
        if self.capacity.get() > 0 {
            (self.capacity.get() as usize, Sign::Positive)
        } else {
            // wrapping will never happen because MAX_CAPACITY < isize::MAX
            (self.capacity.get().wrapping_neg() as usize, Sign::Negative)
        }
    }

    /// Set the sign flag and return the changed representation. The sign will not
    /// be flipped if self is zero
    #[inline]
    pub const fn with_sign(mut self, sign: Sign) -> Self {
        let is_positive = match sign {
            Sign::Positive => true,
            Sign::Negative => false,
        };
        if !self.is_zero() && (is_positive ^ (self.capacity.get() > 0)) {
            self.capacity = unsafe {
                // SAFETY: capacity is not allowed to be zero
                NonZeroIsize::new_unchecked(-self.capacity.get())
            }
        }
        self
    }

    /// Cast the reference of `Repr` to a strong typed representation, assuming the underlying data is unsigned.
    ///
    /// # Panics
    ///
    /// Panics if the `capacity` is negative
    #[inline]
    pub fn as_typed(&self) -> TypedReprRef<'_> {
        let (sign, typed) = self.as_sign_typed();
        assert!(sign == Sign::Positive);
        typed
    }

    /// Cast the reference of `Repr` to a strong typed representation, and return with the sign.
    #[inline]
    pub fn as_sign_typed(&self) -> (Sign, TypedReprRef<'_>) {
        let (abs_capacity, sign) = self.sign_capacity();

        let typed = unsafe {
            match abs_capacity {
                1 | 2 => {
                    TypedReprRef::RefSmall(double_word(self.data.inline[0], self.data.inline[1]))
                }
                _ => TypedReprRef::RefLarge(slice::from_raw_parts(
                    self.data.heap.0,
                    self.data.heap.1,
                )),
            }
        };
        (sign, typed)
    }

    /// Cast the `Repr` to a strong typed representation, assuming the underlying data is unsigned.
    ///
    /// # Panics
    ///
    /// Panics if the `capacity` is negative
    #[inline]
    pub fn into_typed(self) -> TypedRepr {
        assert!(self.capacity.get() > 0);

        unsafe {
            match self.capacity.get() {
                1 | 2 => TypedRepr::Small(double_word(self.data.inline[0], self.data.inline[1])),
                _ => {
                    // SAFETY: An `Buffer` and `Repr` have the same layout
                    //     and we have made sure that the data is allocated on heap
                    TypedRepr::Large(mem::transmute(self))
                }
            }
        }
    }

    /// Cast the `Repr` to a strong typed representation and return with the sign.
    #[inline]
    pub fn into_sign_typed(mut self) -> (Sign, TypedRepr) {
        let (abs_capacity, sign) = self.sign_capacity();
        self.capacity = unsafe {
            // SAFETY: capacity is not allowed to be zero
            NonZeroIsize::new_unchecked(abs_capacity as isize)
        };
        (sign, self.into_typed())
    }

    /// Get a reference to the words in the `Repr`
    ///
    /// # Panics
    ///
    /// Panics if the `capacity` is negative
    #[inline]
    pub fn as_slice(&self) -> &[Word] {
        let (sign, slice) = self.as_sign_slice();
        assert!(sign == Sign::Positive);
        slice
    }

    /// Get a reference to the words in the `Repr`, together with the sign.
    pub fn as_sign_slice(&self) -> (Sign, &[Word]) {
        let (capacity, sign) = self.sign_capacity();
        let words = unsafe {
            match capacity {
                0 => unreachable!(),
                1 => {
                    if self.data.inline[0] == 0 {
                        &[]
                    } else {
                        &self.data.inline[..1]
                    }
                }
                2 => &self.data.inline,
                _ => slice::from_raw_parts(self.data.heap.0, self.data.heap.1),
            }
        };
        (sign, words)
    }

    #[cfg(feature = "zeroize")]
    pub fn as_full_slice(&mut self) -> &mut [Word] {
        unsafe {
            let capacity = self.capacity();
            if capacity <= 2 {
                &mut self.data.inline
            } else {
                slice::from_raw_parts_mut(self.data.heap.0, capacity)
            }
        }
    }

    /// Creates a `Repr` with a single word
    #[inline]
    pub const fn from_word(n: Word) -> Self {
        Repr {
            data: ReprData { inline: [n, 0] },
            capacity: unsafe { NonZeroIsize::new_unchecked(1) },
        }
    }

    /// Creates a `Repr` with a double word
    #[inline]
    pub const fn from_dword(n: DoubleWord) -> Self {
        let (lo, hi) = split_dword(n);
        Repr {
            data: ReprData { inline: [lo, hi] },
            capacity: unsafe { NonZeroIsize::new_unchecked(1 + (hi != 0) as isize) },
        }
    }

    /// Create a `Repr` with a buffer allocated on heap. The leading zeros in the buffer
    /// will be trimmed and the buffer will be shrunk if there is exceeded capacity.
    pub fn from_buffer(mut buffer: Buffer) -> Self {
        buffer.pop_zeros();

        match buffer.len() {
            0 => Self::from_word(0),
            1 => Self::from_word(buffer[0]),
            2 => Self::from_dword(double_word(buffer[0], buffer[1])),
            _ => {
                // If the Buffer was allocated with `Buffer::allocate(n)`
                // and the normalized length is between `n - 2` and `n + 2`
                // (or even approximately between `0.9 * n` and `1.125 * n`),
                // there will be no reallocation here.
                buffer.shrink_to_fit();

                // SAFETY: the length has been checked and capacity >= lenght,
                //         so capacity is nonzero and larger than 2
                unsafe { mem::transmute(buffer) }
            }
        }
    }

    /// Create a [Repr] cloned from a reference to another [Repr]
    pub fn from_ref(tref: TypedReprRef) -> Self {
        match tref {
            TypedReprRef::RefSmall(dw) => Self::from_dword(dw),
            TypedReprRef::RefLarge(words) => Self::from_buffer(Buffer::from(words)),
        }
    }

    /// Cast the `Repr` to a [Buffer] instance, assuming the underlying data is unsigned.
    ///
    /// # Panics
    ///
    /// Panics if the `capacity` is negative
    pub fn into_buffer(self) -> Buffer {
        assert!(self.capacity.get() > 0);

        unsafe {
            match self.capacity.get() {
                1 => {
                    let mut buffer = Buffer::allocate(1);
                    if self.data.inline[0] != 0 {
                        buffer.push(self.data.inline[0]);
                    }
                    buffer
                }
                2 => {
                    debug_assert!(self.data.inline[1] != 0);
                    let mut buffer = Buffer::allocate(2);
                    buffer.push(self.data.inline[0]);
                    buffer.push(self.data.inline[1]);
                    buffer
                }
                _ => {
                    // SAFETY: An `Buffer` and `Repr` have the same layout
                    //     and we have made sure that the data is allocated on heap
                    mem::transmute(self)
                }
            }
        }
    }

    /// Creates a `Repr` with value 0
    #[inline]
    pub const fn zero() -> Self {
        Self::from_word(0)
    }

    /// Check if the underlying value is zero
    #[inline]
    pub const fn is_zero(&self) -> bool {
        self.capacity() == 1 && unsafe { self.data.inline[0] == 0 }
    }

    /// Creates a `Repr` with value 1
    #[inline]
    pub const fn one() -> Self {
        Self::from_word(1)
    }

    /// Check if the underlying value is zero
    #[inline]
    pub const fn is_one(&self) -> bool {
        self.capacity.get() == 1 && unsafe { self.data.inline[0] == 1 }
    }

    /// Creates a `Repr` with value -1
    #[inline]
    pub const fn neg_one() -> Self {
        Self::from_word(1).with_sign(Sign::Negative)
    }

    /// Flip the sign bit of the Repr and return it
    pub const fn neg(mut self) -> Self {
        if !self.is_zero() {
            self.capacity = unsafe { NonZeroIsize::new_unchecked(-self.capacity.get()) }
        }
        self
    }

    /// Returns a number representing sign of self.
    ///
    /// * [Self::zero] if the number is zero
    /// * [Self::one] if the number is positive
    /// * [Self::neg_one] if the number is negative
    pub const fn signum(&self) -> Self {
        if self.is_zero() {
            Self::zero()
        } else if self.capacity.get() < 0 {
            Self::neg_one()
        } else {
            Self::one()
        }
    }
}

// Cloning for Repr is written in a verbose way because it's performance critical.
impl Clone for Repr {
    fn clone(&self) -> Self {
        let (capacity, sign) = self.sign_capacity();

        let new = unsafe {
            // inline the data if the length is less than 3
            // SAFETY: we check the capacity before accessing the variants
            if capacity <= 2 {
                Repr {
                    data: ReprData {
                        inline: self.data.inline,
                    },
                    capacity: NonZeroIsize::new_unchecked(capacity as isize),
                }
            } else {
                let (ptr, len) = self.data.heap;
                let mut new_buffer = Buffer::allocate(len);
                new_buffer.push_slice(slice::from_raw_parts(ptr, len));

                // SAFETY: abs(self.capacity) >= 3 => self.data.len >= 3
                // so the capacity and len of new_buffer will be both >= 3
                mem::transmute(new_buffer)
            }
        };
        new.with_sign(sign)
    }

    fn clone_from(&mut self, src: &Self) {
        let (src_cap, src_sign) = src.sign_capacity();
        let (cap, _) = self.sign_capacity();

        unsafe {
            // shortcut for inlined data
            if src_cap <= 2 {
                if cap > 2 {
                    // release the old buffer if necessary
                    // SAFETY: self.data.heap.0 must be valid pointer if cap > 2
                    Buffer::deallocate_raw(NonNull::new_unchecked(self.data.heap.0), cap);
                }
                self.data.inline = src.data.inline;
                self.capacity = src.capacity;
                return;
            }

            // SAFETY: we checked that abs(src.capacity) > 2
            let (src_ptr, src_len) = src.data.heap;
            debug_assert!(src_len >= 3);

            // check if we need reallocation, it happens when capacity is too small or too large
            if cap < src_len || cap > Buffer::max_compact_capacity(src_len) {
                if cap > 2 {
                    // release the old buffer if necessary
                    Buffer::deallocate_raw(NonNull::new_unchecked(self.data.heap.0), cap);
                }

                let new_cap = Buffer::default_capacity(src_len);
                let new_ptr = Buffer::allocate_raw(new_cap);
                self.data.heap.0 = new_ptr.as_ptr();
                // SAFETY: allocate_raw will allocates at least 2 words even if src_len is 0
                self.capacity = NonZeroIsize::new_unchecked(new_cap as isize);
            }

            // SAFETY: src.ptr and self.ptr are both properly allocated by `Buffer::allocate()`.
            //         src.ptr and self.ptr cannot alias, because the ptr should be uniquely owned by the Buffer
            ptr::copy_nonoverlapping(src_ptr, self.data.heap.0, src_len);

            // update length and sign
            self.data.heap.1 = src_len;
            if (src_sign == Sign::Positive) ^ (self.capacity.get() > 0) {
                self.capacity = NonZeroIsize::new_unchecked(-self.capacity.get());
            }
        }
    }
}

impl Drop for Repr {
    fn drop(&mut self) {
        let cap = self.capacity();
        if cap > 2 {
            unsafe {
                Buffer::deallocate_raw(NonNull::new_unchecked(self.data.heap.0), cap);
            }
        }
    }
}

impl PartialEq for Repr {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_sign_slice() == other.as_sign_slice()
    }
}
impl Eq for Repr {}

impl fmt::Debug for Repr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (sign, words) = self.as_sign_slice();
        if let Sign::Negative = sign {
            f.write_char('-')?;
        }
        f.debug_list().entries(words).finish()
    }
}

impl Hash for Repr {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let (sign, arr) = self.as_sign_slice();
        sign.hash(state);
        (*arr).hash(state);
    }
}

impl TypedRepr {
    /// Convert a reference of `TypedRef` to `TypedReprRef`
    #[inline]
    pub fn as_ref(&self) -> TypedReprRef {
        match self {
            Self::Small(dword) => TypedReprRef::RefSmall(*dword),
            Self::Large(words) => TypedReprRef::RefLarge(words),
        }
    }
}

impl<'a> TypedReprRef<'a> {
    /// Get the length of the number in words, return 0 when the number is zero.
    #[inline]
    pub fn len(&self) -> usize {
        match self {
            Self::RefSmall(dword) => {
                if *dword == 0 {
                    0
                } else if *dword <= Word::MAX as DoubleWord {
                    1
                } else {
                    2
                }
            }
            Self::RefLarge(words) => words.len(),
        }
    }

    /// This operation just return a copy of `self`. It's meant to be used in macros.
    #[inline]
    pub fn as_ref(&self) -> TypedReprRef {
        *self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::primitive::WORD_BITS_USIZE;

    #[test]
    fn test_inline() {
        let repr = Repr::zero();
        assert_eq!(repr.capacity(), 1);
        assert_eq!(repr.len(), 0);

        let repr = Repr::from_word(123);
        assert_eq!(repr.capacity(), 1);
        assert_eq!(repr.len(), 1);

        let repr = Repr::from_dword(123 << WORD_BITS_USIZE);
        assert_eq!(repr.capacity(), 2);
        assert_eq!(repr.len(), 2);
    }

    #[test]
    fn test_deref() {
        let repr = Repr::zero();
        assert_eq!(repr.as_sign_slice(), (Sign::Positive, &[][..]));

        let repr = Repr::one();
        assert_eq!(repr.as_slice(), &[1][..]);
        assert_eq!(repr.as_sign_slice(), (Sign::Positive, &[1][..]));

        let mut buffer = Buffer::allocate(1);
        buffer.push(1);
        let repr = Repr::from_buffer(buffer).with_sign(Sign::Negative);
        assert_eq!(repr.as_sign_slice(), (Sign::Negative, &[1][..]));

        let mut buffer = Buffer::allocate(2);
        buffer.push(1);
        buffer.push(2);
        let repr = Repr::from_buffer(buffer);
        assert_eq!(repr.as_slice(), &[1, 2][..]);
        assert_eq!(repr.as_sign_slice(), (Sign::Positive, &[1, 2][..]));

        let mut buffer = Buffer::allocate(2);
        buffer.push(1);
        buffer.push(2);
        buffer.push(3);
        buffer.push(4);
        let repr = Repr::from_buffer(buffer);
        assert_eq!(repr.as_slice(), &[1, 2, 3, 4][..]);
        assert_eq!(repr.as_sign_slice(), (Sign::Positive, &[1, 2, 3, 4][..]));
    }

    #[test]
    fn test_sign() {
        let repr = Repr::zero();
        assert_eq!(repr.sign(), Sign::Positive);
        let repr = Repr::zero().neg();
        assert_eq!(repr.sign(), Sign::Positive);

        let repr = Repr::one();
        assert_eq!(repr.sign(), Sign::Positive);
        let repr = Repr::one().neg();
        assert_eq!(repr.sign(), Sign::Negative);
    }

    #[test]
    fn test_clone() {
        // test Repr
        let repr = Repr::from_word(123);
        let repr2 = repr.clone();
        assert_eq!(repr2.capacity(), 1);
        assert_eq!(repr2.len(), 1);
        assert_eq!(repr, repr2);

        let repr = Repr::from_dword(123 << WORD_BITS_USIZE);
        let repr2 = repr.clone();
        assert_eq!(repr2.capacity(), repr.capacity());
        assert_eq!(repr2.len(), repr.len());
        assert_eq!(repr, repr2);

        // test Buffer
        let mut buffer = Buffer::allocate(100);
        buffer.push(7);
        buffer.push(8);
        buffer.push(9);
        let buffer2 = buffer.clone();
        assert_eq!(buffer, buffer2);
        assert_eq!(buffer2.capacity(), Buffer::default_capacity(3));

        let repr = Repr::from_buffer(buffer);
        let repr2 = repr.clone();
        assert_eq!(repr.capacity(), Buffer::default_capacity(3));
        assert_eq!(repr, repr2);
    }

    #[test]
    fn test_convert_buffer() {
        let buffer = Buffer::allocate(0);
        let repr = Repr::from_buffer(buffer);
        assert_eq!(repr.len(), 0);
        assert!(repr.as_slice().is_empty());
        let buffer_back = repr.into_buffer();
        assert_eq!(buffer_back.len(), 0);
        assert!(buffer_back.is_empty());

        let mut buffer = Buffer::allocate(1);
        buffer.push(123);
        let repr = Repr::from_buffer(buffer);
        assert_eq!(repr.len(), 1);
        assert_eq!(repr.as_slice(), &[123][..]);
        let buffer_back = repr.into_buffer();
        assert_eq!(buffer_back.len(), 1);
        assert_eq!(&buffer_back[..], &[123][..]);

        let mut buffer = Buffer::allocate(2);
        buffer.push(123);
        buffer.push(456);
        let repr = Repr::from_buffer(buffer);
        assert_eq!(repr.len(), 2);
        assert_eq!(repr.as_slice(), &[123, 456][..]);
        let buffer_back = repr.into_buffer();
        assert_eq!(buffer_back.len(), 2);
        assert_eq!(&buffer_back[..], &[123, 456][..]);

        let mut buffer = Buffer::allocate(3);
        buffer.push(123);
        buffer.push(456);
        buffer.push(789);
        let repr = Repr::from_buffer(buffer);
        assert_eq!(repr.len(), 3);
        assert_eq!(repr.as_slice(), &[123, 456, 789][..]);
        let buffer_back = repr.into_buffer();
        assert_eq!(buffer_back.len(), 3);
        assert_eq!(&buffer_back[..], &[123, 456, 789][..]);
    }

    #[test]
    fn test_clone_from() {
        // test Repr
        let repr = Repr::from_word(123);
        let mut repr2 = Repr::zero();
        repr2.clone_from(&repr);
        assert_eq!(repr2.capacity(), repr.capacity());
        assert_eq!(repr2.len(), repr.len());
        assert_eq!(repr, repr2);

        let repr = Repr::from_dword(123 << WORD_BITS_USIZE);
        let mut repr2 = Repr::zero();
        repr2.clone_from(&repr);
        assert_eq!(repr2.capacity(), repr.capacity());
        assert_eq!(repr2.len(), repr.len());
        assert_eq!(repr, repr2);

        // test Buffer
        let mut buffer = Buffer::allocate(100);
        buffer.push(7);
        buffer.push(8);
        buffer.push(9);
        let mut buffer2 = Buffer::allocate(50);
        buffer2.clone_from(&buffer);
        assert_eq!(buffer, buffer2);
        assert_ne!(buffer.capacity(), buffer2.capacity());

        let repr = Repr::from_buffer(buffer);
        let mut repr2 = Repr::from_buffer(buffer2);
        repr2.clone_from(&repr);
        assert_eq!(repr, repr2);
    }

    #[test]
    fn test_resizing_clone_from() {
        // test Buffer
        let mut buf = Buffer::allocate(5);
        assert_eq!(buf.capacity(), 7);

        let mut buf2 = Buffer::allocate(4);
        assert_eq!(buf2.capacity(), 6);
        for i in 0..4 {
            buf2.push(i);
        }
        buf.clone_from(&buf2);
        assert_eq!(buf.capacity(), 7);
        assert_eq!(&buf[..], [0, 1, 2, 3]);

        let mut buf3 = Buffer::allocate(100);
        for i in 0..100 {
            buf3.push(i);
        }
        buf.clone_from(&buf3);
        assert_eq!(buf.capacity(), Buffer::default_capacity(100));
        assert_eq!(buf.len(), 100);

        buf.clone_from(&buf2);
        assert_eq!(buf.capacity(), 6);
        assert_eq!(&buf[..], [0, 1, 2, 3]);

        // test Repr
        let mut repr = Repr::zero(); // start from inline
        let repr2 = Repr::from_buffer(buf2);
        repr.clone_from(&repr2);
        assert_eq!(repr.len(), 4);
        assert_eq!(repr, repr2);
        assert!(matches!(repr.as_typed(), TypedReprRef::RefLarge(_)));

        let repr3 = Repr::from_buffer(buf3);
        repr.clone_from(&repr3);
        assert_eq!(repr.len(), 100);
        assert_eq!(repr, repr3);
        assert!(matches!(repr.as_typed(), TypedReprRef::RefLarge(_)));

        repr.clone_from(&repr2);
        assert_eq!(repr.len(), 4);
        assert_eq!(repr, repr2);
        assert!(matches!(repr.as_typed(), TypedReprRef::RefLarge(_)));

        let repr_inline = Repr::from_word(123);
        repr.clone_from(&repr_inline);
        assert_eq!(repr.len(), 1);
        assert_eq!(repr, repr_inline);
        assert!(matches!(repr.as_typed(), TypedReprRef::RefSmall(_)));
    }
}