简体   繁体   English

通用边界确保非引用参数

[英]Generic bound ensuring non-reference argument

The real reason to do this is beyond the scope, I just can say that it's related to a very unusual memory layout on some embedded platform.这样做的真正原因超出了 scope,我只能说这与某些嵌入式平台上非常不寻常的 memory 布局有关。 The goal is to make a wrapper for statics which can do some manual address translation on dereferencing.目标是为静态做一个包装器,它可以在取消引用时做一些手动地址转换。 So putting a reference inside is pointless and unsafe.所以在里面放一个引用是没有意义的,也是不安全的。 Alternatively I can make two implementations for references and not references but they must be strongly unambiguous.或者,我可以为引用和非引用创建两个实现,但它们必须非常明确。

You can do this but it requires the nightly features negative_impls and auto_traits :你可以这样做,但它需要夜间功能negative_implsauto_traits

#![feature(negative_impls)]
#![feature(auto_traits)]

auto trait NotRawRef {}

impl<T> !NotRawRef for &T {}
impl<T> !NotRawRef for &mut T {}

fn foo<T: NotRawRef>(_: T) {}

This will apply recursively by default , so T cannot be a user-defined type that contains a reference -- unless the user-defined type explicitly implements NotRawRef .这将默认递归应用,因此T不能是包含引用的用户定义类型——除非用户定义类型显式实现NotRawRef You have to trust callers of your function not to implement this trait when it's not appropriate.你必须相信你的 function 的呼叫者不会在不合适的时候实现这个特性。


If nightly isn't an option, or if this is too strict, you can rather easily bound on "if there are any references, they must have static lifetime" with 'static .如果 nightly 不是一个选项,或者如果这太严格,您可以很容易地使用'static绑定“如果有任何引用,它们必须具有 static 生命周期”。 For example:例如:

fn foo<T: 'static>(_: T) { }

fn main() {
    let x = 1;
    foo(x);

    // The following line would fail with E0597:
    // foo(&x);

    // Works because the reference has static lifetime
    foo(&1);
}

This bound applies to user-defined types that have generic lifetime parameters as well, and will reject any such type with a non- 'static lifetime argument.此绑定也适用于具有通用生命周期参数的用户定义类型,并且将拒绝任何具有非'static生命周期参数的此类类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将生存期约束添加到非引用类型 - Adding lifetime constraints to non-reference types 非引用类型是否始终满足“静态”生存期? - Do non-reference types always satisfy a lifetime of 'static? 在为引用和非引用类型实现一个特性时,我是否必须实现它两次? - Do I have to implement a trait twice when implementing it for both reference and non-reference types? 在 Rust 中将非引用与引用进行模式匹配有什么作用? - What does pattern-matching a non-reference against a reference do in Rust? 带引用的通用参数用作函数指针参数 - Generic parameter with reference used as function pointer argument 如何将特征绑定到非泛型类型? - How to add trait bound to a non-generic type? 如果在函数内创建引用,如何将泛型类型与需要使用生命周期参数的特征绑定在一起? - How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function? 在 Rust 中,当对作为通用参数传递的值进行装箱时,为什么需要“静态”生命周期限制? - In Rust, when boxing a value passed as a generic argument, why is a `'static` lifetime bound required? 性状受制于普通特征 - Trait bound on generic trait 为什么我可以将对非静态局部变量的引用传递给具有“静态绑定”的函数? - Why can I pass a reference to a non-static local variable to a function which has a 'static bound?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM