owning_ref

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

Latest version: 0.4.1 registry icon
Maintenance score
0
Safety score
0
Popularity score
77
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
0.4.1 0 0 1 0 0
0.4.0 0 0 1 0 0
0.3.3 0 0 1 0 0
0.3.2 0 0 1 0 0
0.3.1 0 0 1 0 0
0.3.0 0 0 1 0 0
0.2.4 0 0 1 0 0
0.2.3 0 0 1 0 0
0.2.2 0 0 1 0 0
0.2.1 0 0 1 0 0
0.2.0 0 0 1 0 0
0.1.4 0 0 1 0 0
0.1.3 0 0 1 0 0
0.1.2 0 0 1 0 0
0.1.1 0 0 1 0 0
0.1.0 0 0 1 0 0

Stability
Latest release:

0.4.1 - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



Build Status Crate Docs

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
}

Getting Started

To get started, add the following to Cargo.toml.

owning_ref = "0.4.1"

...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!
}