我想将一个引用的元组(所有引用都指向同一结构的成员)转换为一个元组的引用。
我试图以各种方式强迫他们,但是如果没有克隆,我是无法做到的。
struct Bar();
struct Foo(Bar, Bar, Bar);
fn main() {
let a: &Foo = &Foo(Bar(), Bar(), Bar());
let b: &(Bar, Bar) = &(a.0, a.1);
}
Run Code Online (Sandbox Code Playgroud)
error[E0507]: cannot move out of borrowed content
--> src/main.rs:7:28
|
7 | let b: &(Bar, Bar) = &(a.0, a.1);
| ^^^ cannot move out of borrowed content
error[E0507]: cannot move out of borrowed content
--> src/main.rs:7:33
|
7 | let b: &(Bar, Bar) = &(a.0, a.1);
| ^^^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)
我期望b是type,&(Bar, Bar)因为atype是&Foo。
这是不可能的。
引用是指一个值。您希望有一个,&(Bar, Bar)但是内存中没有任何地方有2个元组(Bar, Bar)。您不能引用不存在的内容。
的内存布局&(A, B)和(&A, &B)是根本不相容的,所以你也不能使用不安全的防锈技术。
在这种情况下,您可能可以使用不安全的Rust将您&Foo直接转换为&(Bar, Bar),但是...
// I copied this unsafe block from Stack Overflow
// without properly documenting why I think this code is safe.
let b: &(Bar, Bar) = unsafe { &*(a as *const Foo as *const (Bar, Bar)) };
println!("{:?}", b);
Run Code Online (Sandbox Code Playgroud)
// I copied this unsafe block from Stack Overflow
// without properly documenting why I think this code is safe.
let c: &(Bar, Bar) = unsafe {
let p = a as *const Foo as *const Bar;
let p = p.offset(1);
&*(p as *const (Bar, Bar))
};
println!("{:?}", c);
Run Code Online (Sandbox Code Playgroud)
1-实际上,该参考明确指出:
元组对其布局没有任何保证。
例外情况是单位元组(
()),它被保证为零大小类型,其大小为0,对齐方式为1。
这意味着尽管此代码可能会打印出您期望的内容,并且Miri不会抱怨,但这是未定义的行为。
| 归档时间: |
|
| 查看次数: |
136 次 |
| 最近记录: |