- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
Is there a way to make an immutable reference mutable?
(2 个回答)
Why is transmuting &T to &mut T Undefined Behaviour?
(2 个回答)
How to avoid writing duplicate accessor functions for mutable and immutable references in Rust?
(4 个回答)
1年前关闭。
我有一个自定义迭代器,它从对 Vec
的引用中读取数据以非连续方式。它永远不会返回对同一内存的两个引用,所以我相信实现一个可变的对应物是安全的。
然而,迭代器本身的逻辑是相当复杂的,对于可变版本来说它是完全一样的。为了避免代码重复,我在底层使用我的非 mut 迭代器,然后转换结果以增加可变性。这按预期工作,但我不确定它是否在调用某种未定义的行为。
pub mod lib {
pub struct Data {
pub v: Vec<u64>,
}
impl Data {
pub fn iter(&self) -> MyIter {
MyIter {
data: &self.v,
i: 0,
}
}
pub fn iter_mut(&mut self) -> MyIterMut {
MyIterMut { iter: self.iter() }
}
}
pub struct MyIter<'a> {
data: &'a [u64],
i: usize,
}
impl<'a> Iterator for MyIter<'a> {
type Item = &'a u64;
fn next(&mut self) -> Option<Self::Item> {
if self.i == self.data.len() {
return None;
}
let res = &self.data[self.i];
self.i += 1;
Some(res)
}
}
pub struct MyIterMut<'a> {
iter: MyIter<'a>,
}
impl<'a> Iterator for MyIterMut<'a> {
type Item = &'a mut u64;
fn next(&mut self) -> Option<Self::Item> {
unsafe { std::mem::transmute(self.iter.next()) }
}
}
}
fn main() {
let mut d = lib::Data { v: vec![0; 8] };
for elem in d.iter_mut() {
*elem = 123;
}
println!("{:?}", d.v);
}
Complete working example
iter_mut
中构造。方法。这意味着由于
&mut self
,初始值将始终作为可变变量开始。 .不可能从不可变变量构造这个可变迭代器。
const_cast
避免重复仅在 constness 上有所不同的实现。
最佳答案
如果 MyIter
包含对原始 vec 的不可变引用,则无法转换,因为您可能有多个 MyIter
实例,导致引用冲突。好像是 duplicate
crate 可以在这里使用,它提供了一种复制代码的简单方法,但略有不同。
关于rust - 使用 transmute 实现可变迭代器以避免与不可变对应物的代码重复是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64840552/
我想检查我的应用程序或系统中是否存在库。在 Java 中,我通常执行 System.loadlibrary,但是有谁知道 C 中类似的相应调用吗? 最佳答案 是dlopen打开一个库,dlsym 从加
我在 typescript 中输入以下内容 export type Excludable = T & { isExcluded?: boolean } 其中过滤值: export type Filte
我最近在我的应用程序中添加了一种方法,可以自动格式化 TextView ,从“50000”到“50,000”,效果绝对完美。现在我遇到的问题是,在我的应用程序中,有多个按钮功能可以从该 TextVie
SELECT * FROM conversations WHERE chatMembers LIKE '%1%'AND chatMembers LIKE '%10%' 对话表 id | chatMem
我正在编写一个需要将 Java Date() 值保存到 MySQL 数据库的 RESTful Web 服务,但是,我不确定 MySQL 中可以保存 Java Date() 的数据类型是什么,或者我是否
同样,在任何 Red Hat 或 JBoss 站点上都没有关于此的信息,所以我在这里问... 我不确定是 13 还是 14。 最佳答案 Mapping the Community versions w
同样,在任何 Red Hat 或 JBoss 站点上都没有关于此的信息,所以我在这里问... 我不确定是 13 还是 14。 最佳答案 Mapping the Community versions w
我曾尝试使用 swift 开发一款利用 iPhone 的 3D 触摸硬件的游戏。然而,当我将我的应用程序提交到 App Store 时,它被拒绝了,因为该游戏无法在 iPad 上玩。 我的问题是,
Qt 的有序关联容器对应项 std::map是QMap , std::set是QSet , 对于无序关联容器 std::unordered_map是QHash . 我应该用什么来代替std::unor
JavaScript 方法 String.fromCharCode() 在以下意义上与 Python 的 unichar() 等效: print unichr(213) # prints Õ on t
正如谷歌在 "Discontinuing support for JSON-RPC and Global HTTP Batch Endpoints" 中提到的那样,Google API 客户端库已重新
我正在使用 MapLayer 和 MapOverlay 在 map 中创建自己的路径/折线,GPS 捕获的所有点都存储在一个结构中,以便我可以访问它们。随时。 现在,我希望路径在用户操作 map (缩
我们使用 Adobe Flash Builder 创建由 Flex 提供支持的交互式 Web 应用程序。现在我们正在寻找替代方案,让我们在 UI 设计和迎合 HTML5 的编码方面拥有同样的开发便
我想知道Android/Java 中类似C#/C++ 中的GetTickCount 方法的相应方法吗? 最佳答案 Android 为 SystemClock.uptimeMillis() .请注意,u
我用 Vue + Phaser 开始了新项目,但是当我尝试加载 Assets 时,this.game.load.image 中的“load”和“add”返回“undefined”。我尝试从 JS 文件
我是一名优秀的程序员,十分优秀!