Enum std::borrow::Cow 1.0.0[−][src]
pub enum Cow<'a, B> where
B: 'a + ToOwned + ?Sized, { Borrowed(&'a B), Owned(<B as ToOwned>::Owned), }
Expand description
写入时克隆智能指针。
Cow
类型是一种智能指针,提供了写时克隆功能: 它可以封装并提供对借用数据的不可变访问,并在需要可变的或所有权时懒惰地克隆数据。
该类型旨在通过 Borrow
trait 处理常规借用数据。
Cow
实现 Deref
,这意味着您可以直接在其所包含的数据上调用非可变方法。
如果需要进行可变的,则 to_mut
将获得一个拥有值的变量引用,必要时进行克隆。
如果需要引用计数指针,请注意 Rc::make_mut
和 Arc::make_mut
也可以提供写时克隆功能。
Examples
use std::borrow::Cow; fn abs_all(input: &mut Cow<[i32]>) { for i in 0..input.len() { let v = input[i]; if v < 0 { // 如果尚未拥有,则克隆到 vector 中。 input.to_mut()[i] = -v; } } } // 因为 `input` 不需要可变的,所以不会发生克隆。 let slice = [0, 1, 2]; let mut input = Cow::from(&slice[..]); abs_all(&mut input); // 发生克隆是因为需要对 `input` 进行可变的。 let slice = [-1, 0, 1]; let mut input = Cow::from(&slice[..]); abs_all(&mut input); // 因为 `input` 已被拥有,所以不会发生克隆。 let mut input = Cow::from(vec![-1, 0, 1]); abs_all(&mut input);Run
另一个示例显示如何将 Cow
保留在结构体中:
use std::borrow::Cow; struct Items<'a, X: 'a> where [X]: ToOwned<Owned = Vec<X>> { values: Cow<'a, [X]>, } impl<'a, X: Clone + 'a> Items<'a, X> where [X]: ToOwned<Owned = Vec<X>> { fn new(v: Cow<'a, [X]>) -> Self { Items { values: v } } } // 根据切片的借用值创建容器 let readonly = [1, 2]; let borrowed = Items::new((&readonly[..]).into()); match borrowed { Items { values: Cow::Borrowed(b) } => println!("borrowed {:?}", b), _ => panic!("expect borrowed value"), } let mut clone_on_write = borrowed; // 将切片中的数据可变的为拥有的 vec,并在顶部推入新值 clone_on_write.values.to_mut().push(3); println!("clone_on_write = {:?}", clone_on_write.values); // 数据被可变的。让我们看看。 match clone_on_write { Items { values: Cow::Owned(_) } => println!("clone_on_write contains owned data"), _ => panic!("expect owned data"), }Run
Variants
Borrowed(&'a B)
借用的数据。
拥有的数据。
Implementations
提取拥有的数据。
如果尚未拥有该数据,则将其克隆。
Examples
在 Cow::Borrowed
上调用 into_owned
会克隆基础数据并成为 Cow::Owned
:
use std::borrow::Cow; let s = "Hello world!"; let cow = Cow::Borrowed(s); assert_eq!( cow.into_owned(), String::from(s) );Run
禁止在 Cow::Owned
上调用 into_owned
:
use std::borrow::Cow; let s = "Hello world!"; let cow: Cow<str> = Cow::Owned(String::from(s)); assert_eq!( cow.into_owned(), String::from(s) );Run
Trait Implementations
执行 +=
操作。 Read more
执行 +=
操作。 Read more
用一个元素扩展一个集合。
pub fn from(cow: Cow<'_, [T]>) -> Box<[T], Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
pub fn from(cow: Cow<'_, [T]>) -> Box<[T], Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
执行转换。
fn from(cow: Cow<'_, CStr>) -> Box<CStr>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
fn from(cow: Cow<'_, CStr>) -> Box<CStr>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
执行转换。
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
fn from(cow: Cow<'_, OsStr>) -> Box<OsStr>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
执行转换。
fn from(cow: Cow<'_, Path>) -> Box<Path>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
fn from(cow: Cow<'_, Path>) -> Box<Path>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
从写时克隆指针创建一个 boxed Path
。
从 Cow::Owned
转换不会克隆或分配。
pub fn from(cow: Cow<'_, str>) -> Box<str, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
pub fn from(cow: Cow<'_, str>) -> Box<str, Global>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
执行转换。
fn from(err: Cow<'a, str>) -> Box<dyn Error>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
fn from(err: Cow<'a, str>) -> Box<dyn Error>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
将 Cow
转换为 Dyn Error
+ Send
+ Sync
的 box。
Examples
use std::error::Error; use std::mem; use std::borrow::Cow; let a_cow_str_error = Cow::from("a str error"); let a_boxed_error = Box::<dyn Error + Send + Sync>::from(a_cow_str_error); assert!( mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))Run