owning-ref-rs

A library for creating references that carry their owner with them.

  • 所有者: Kimundi/owning-ref-rs
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

owning-ref-rs

A library for creating references that carry their owner with them.

This can sometimes be useful because Rust borrowing rules normally prevent
moving a type that has been borrowed from. For example, this kind of code gets rejected:

fn return_owned_and_referenced<'a>() -> (Vec<u8>, &'a [u8]) {
    let v = vec![1, 2, 3, 4];
    let s = &v[1..3];
    (v, s)
}

This library enables this safe usage by keeping the owner and the reference
bundled together in a wrapper type that ensure that lifetime constraint:

fn return_owned_and_referenced() -> OwningRef<Vec<u8>, [u8]> {
    let v = vec![1, 2, 3, 4];
    let or = OwningRef::new(v);
    let or = or.map(, v, &v[1..3]);
    or
}

Travis-CI Status

Getting Started

owning-ref-rs is available on crates.io.
It is recommended to look there for the newest released version, as well as links to the newest builds of the docs.

At the point of the last update of this README, the latest published version could be used like this:

Add the following dependency to your Cargo manifest...

[dependencies]
owning_ref = "0.4"

...and see the docs for how to use it.

Example

extern crate owning_ref;
use owning_ref::BoxRef;

fn main() {
    // Create an array owned by a Box.
    let arr = Box::new([1, 2, 3, 4]) as Box<[i32]>;

    // Transfer into a BoxRef.
    let arr: BoxRef<[i32]> = BoxRef::new(arr);
    assert_eq!(&*arr, &[1, 2, 3, 4]);

    // We can slice the array without losing ownership or changing type.
    let arr: BoxRef<[i32]> = arr.map(, arr, &arr[1..3]);
    assert_eq!(&*arr, &[2, 3]);

    // Also works for Arc, Rc, String and Vec!
}

主要指标

概览
名称与所有者Kimundi/owning-ref-rs
主编程语言Rust
编程语言Rust (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-07-26 08:42:30
推送于2023-10-12 09:58:38
最后一次提交2020-02-27 16:46:39
发布数0
用户参与
星数376
关注者数10
派生数52
提交数83
已启用问题?
问题数42
打开的问题数23
拉请求数18
打开的拉请求数12
关闭的拉请求数9
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?