Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第一百页,标准库变化(NonZero 已经废弃了?) #42

Open
slanterns opened this issue Jan 10, 2020 · 1 comment
Open

第一百页,标准库变化(NonZero 已经废弃了?) #42

slanterns opened this issue Jan 10, 2020 · 1 comment

Comments

@slanterns
Copy link

slanterns commented Jan 10, 2020

书中为 pointer: NonZero<*const T>, 然而现有(Nightly 2020/1/9)实现已更改为:

#[rustc_layout_scalar_valid_range_start(1)]
pub struct Unique<T: ?Sized> {
    pointer: *const T,
    _marker: PhantomData<T>,
}

注意上文出现的 rustc_attr, 现在应该是靠这个保证非0从而进行优化。

类似的,现在 rust 提供了 core::ptr::NonNull:

#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_nonnull_optimization_guaranteed]
pub struct NonNull<T: ?Sized> {
    pointer: *const T,
}

也提供了通过宏统一加上 #[rustc_layout_scalar_valid_range_start(1)]#[rustc_nonnull_optimization_guaranteed] 的一系列 core::num::NonZero{IntType} 可以保证非零。未来应该建议用户使用这些提供的非零指针 / 整型进行优化。
举例:let nonZeroA: NonZeroI64 = std::num::NonZeroI64::new(1).unwrap();


查看 rustc commit history 后发现相关更改发生在:rust-lang/rust@7a09115

@F001
Copy link
Owner

F001 commented Jan 11, 2020

是的! 你说的完全正确。

@slanterns slanterns reopened this Jan 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants