#[derive(Clone, Debug)]
pub struct Elem {
inner: u8,
}
#[derive(Clone)]
#[repr(transparent)]
pub struct Dict{
list : Vec<Elem>,
}
impl Dict {
pub fn new() -> Self {
Self {
list: Vec::new()
}
}
pub fn set(&mut self, elem: Elem) {
self.list.push(elem)
}
pub fn len(&self) -> usize {
self.list.len()
}
#[allow(invalid_reference_casting)]
pub fn get_mut_unchecked(&self) -> &mut Self {
unsafe {
let ptr1 = self as *const Self;
let ptr2 = ptr1 as *mut Self;
&mut *ptr2
}
}
}
fn func(dict: &Dict) {
let this = dict.get_mut_unchecked();
let elem = Elem{inner:42};
this.set(elem);
}
fn main() {
let dict = Dict::new();
func(&dict);
println!("Hello, world! => len:{}", dict.len());
}
// --------------------- 我希望能通过回调获取数据,也就是dict.len()!=0; 但是实际上在release模式下传入dict的数据被优化掉了
rust版本:
rustc 1.80.0-nightly (867900499 2024-05-23)
binary: rustc
commit-hash: 8679004993f08807289911d9f400f4ac4391d2bc
commit-date: 2024-05-23
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6
1
共 1 条评论, 1 页
评论区
写评论如果func(dict: &Dict) 改成 func(dict: &mut Dict) 则上述问题就没有了