File: issue-110682.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (92 lines) | stat: -rw-r--r-- 1,874 bytes parent folder | download | duplicates (6)
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
//@ build-pass
//@ compile-flags: -Zmir-opt-level=3

use std::fmt::Debug;
use std::mem::ManuallyDrop;
use std::ptr;

pub trait BitRegister {}

macro_rules! register {
    ($($t:ty),+ $(,)?) => { $(
        impl BitRegister for $t {
        }
    )* };
}

register!(u8, u16, u32);

pub trait BitStore: Sized + Debug {
    /// The register type that the implementor describes.
    type Mem: BitRegister + Into<Self>;
}

macro_rules! store {
    ($($t:ty),+ $(,)?) => { $(
        impl BitStore for $t {
            type Mem = Self;
        }
    )+ };
}

store!(u8, u16, u32,);

#[repr(C)]
pub struct BitVec<T>
where
    T: BitStore,
{
    /// Region pointer describing the live portion of the owned buffer.
    pointer: ptr::NonNull<T>,
    /// Allocated capacity, in elements `T`, of the owned buffer.
    capacity: usize,
}

impl<T> BitVec<T>
where
    T: BitStore,
{
    pub fn new() -> Self {
        let pointer = ptr::NonNull::<T>::new(ptr::null_mut()).unwrap();

        BitVec { pointer, capacity: 10 }
    }

    pub fn clear(&mut self) {
        unsafe {
            self.set_len(0);
        }
    }

    #[inline]
    pub unsafe fn set_len(&mut self, new_len: usize) {}

    fn with_vec<F, R>(&mut self, func: F) -> R
    where
        F: FnOnce(&mut ManuallyDrop<Vec<T::Mem>>) -> R,
    {
        let cap = self.capacity;
        let elts = 10;
        let mut vec = ManuallyDrop::new(unsafe { Vec::from_raw_parts(ptr::null_mut(), elts, cap) });
        let out = func(&mut vec);

        out
    }
}

impl<T> Drop for BitVec<T>
where
    T: BitStore,
{
    #[inline]
    fn drop(&mut self) {
        //  The buffer elements do not have destructors.
        self.clear();
        //  Run the `Vec` destructor to deƤllocate the buffer.
        self.with_vec(|vec| unsafe { ManuallyDrop::drop(vec) });
    }
}

fn main() {
    let bitvec = BitVec::<u32>::new();
}