原文

1.87.0稳定版中有什么

匿名管道

1.87标准库添加了访问匿名管道.这包括集成std::process::Command输入/输出方法.如,合二为一现在相对简单stdoutstderr流,如下,而过去需要额外的线程或相关平台的函数.

use std::process::Command;
use std::io::Read;
let (mut recv, send) = std::io::pipe()?;
let mut command = Command::new("path/to/bin")
    //`stdout`和`stderr`都会写入同一个`将两者结合起来`的管道.
    .stdout(send.try_clone()?)
    .stderr(send)
    .spawn()?;
let mut output = Vec::new();
recv.read_to_end(&mut output)?;
     //重要的是,在进程退出之前,从管道中读取,以避免在程序发出过多`输出`时,填充`操作系统`缓冲.
assert!(command.wait()?.success());

安全架构内部函数

大多数仅因为需要允许目标功能,而不安全的std::arch内部函数现在可在允许这些功能的安全代码中调用.如,以下玩具程序使用手动内部函数实现求和数组,现在可对核心循环使用安全代码.

#![forbid(unsafe_op_in_unsafe_fn)]
use std::arch::x86_64::*;
fn sum(slice: &[u32]) -> u32 {
    #[cfg(target_arch = "x86_64")]
    {
        if is_x86_feature_detected!("avx2") {
    //安全:检测到在`运行时`已允许该功能,因此可安全地调用此函数.
            return unsafe { sum_avx2(slice) };
        }
    }
    slice.iter().sum()
}
#[target_feature(enable = "avx2")]
#[cfg(target_arch = "x86_64")]
fn sum_avx2(slice: &[u32]) -> u32 {
    //安全性:`__m256i`和`u32`有相同`有效性`.
    let (prefix, middle, tail) = unsafe { slice.align_to::<__m256i>() };
    let mut sum = prefix.iter().sum::<u32>();
    sum += tail.iter().sum::<u32>();
     //`核心`循环现在在`1.87`中是完全安全的代码,因为`内部函数`需要匹配目标特征`(avx2)`与`函数定义`.  
    let mut base = _mm256_setzero_si256();
    for e in middle.iter() {
        base = _mm256_add_epi32(base, *e);
    }
    //安全性:`__m256i`和`u32`有相同有效性.
    let base: [u32; 8] = unsafe { std::mem::transmute(base) };
    sum += base.iter().sum::<u32>();
    sum
}

asm!跳转到Rust代码

内联汇编(asm!)现在可跳转到Rust代码中的标签块.这可实现更灵活低级编程,如在操作系统内核中实现优化的控制流更有效地与硬件交互.

1,asm!宏现在支持用作跳转目标标签操作数.
2,标签必须是返回类型()或!块式.

3,该块在跳转到时执行,并在asm!之后继续执行块.
4,在同一个asm!中使用输出标签操作数调用仍不稳定.

unsafe {
    asm!(
        "jmp {}",
        label {
            println!("Jumped from asm!");
        }
    );
}

更多详情

在特征定义中impl Trait的特征的精准捕捉(+用<…>)

此版本稳定了使用impl Trait返回类型,在特征定义指定特定抓模板类型生命期.这允许在特征定义中使用此功能,并在1.82中扩展了非特征函数的稳定性.
一些脱糖示例:

trait Foo {
    fn method<'a>(&'a self) -> impl Sized;
    //...脱糖如下:
    type Implicit1<'a>: Sized;
    fn method_desugared<'a>(&'a self) -> Self::Implicit1<'a>;
    //...而通过精确抓......
    fn precise<'a>(&'a self) -> impl Sized + use<Self>;
    //...脱糖如下:
    type Implicit2: Sized;
    fn precise_desugared<'a>(&'a self) -> Self::Implicit2;
}

稳定的API

Vec::extract_if
vec::ExtractIf
LinkedList::extract_if
linked_list::ExtractIf
<[T]>::split_off
<[T]>::split_off_mut
<[T]>::split_off_first
<[T]>::split_off_first_mut
<[T]>::split_off_last
<[T]>::split_off_last_mut
String::extend_from_within
os_str::Display
OsString::display
OsStr::display
io::pipe
io::PipeReader
io::PipeWriter
impl From<PipeReader> for OwnedHandle
impl From<PipeWriter> for OwnedHandle
impl From<PipeReader> for Stdio
impl From<PipeWriter> for Stdio
impl From<PipeReader> for OwnedFd
impl From<PipeWriter> for OwnedFd
Box<MaybeUninit<T>>::write
impl TryFrom<Vec<u8>> for String
<*const T>::offset_from_unsigned
<*const T>::byte_offset_from_unsigned
<*mut T>::offset_from_unsigned
<*mut T>::byte_offset_from_unsigned
NonNull::offset_from_unsigned
NonNull::byte_offset_from_unsigned
<uN>::cast_signed
NonZero::<uN>::cast_signed.
<iN>::cast_unsigned.
NonZero::<iN>::cast_unsigned.
<uN>::is_multiple_of
<uN>::unbounded_shl
<uN>::unbounded_shr
<iN>::unbounded_shl
<iN>::unbounded_shr
<iN>::midpoint
<str>::from_utf8
<str>::from_utf8_mut
<str>::from_utf8_unchecked
<str>::from_utf8_unchecked_mut

这些以前稳定的API现在在环境中是稳定的:

core::str::from_utf8_mut
<[T]>::copy_from_slice
SocketAddr::set_ip
SocketAddr::set_port,
SocketAddrV4::set_ip
SocketAddrV4::set_port,
SocketAddrV6::set_ip
SocketAddrV6::set_port
SocketAddrV6::set_flowinfo
SocketAddrV6::set_scope_id
char::is_digit
char::is_whitespace
<[[T; N]]>::as_flattened
<[[T; N]]>::as_flattened_mut
String::into_bytes
String::as_str
String::capacity
String::as_bytes
String::len
String::is_empty
String::as_mut_str
String::as_mut_vec
Vec::as_ptr
Vec::as_slice
Vec::capacity
Vec::len
Vec::is_empty
Vec::as_mut_slice
Vec::as_mut_ptr

删除i586-pc-windows-msvc目标

窗口10是所有窗口目标(win7目标除外)的最低要求OS版本,它自身需要SSE2指令.
应迁移至i686-pc-windows-msvc.

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐