#reference #owning #field #sibling

owning_ref

A library for creating references that carry their owner with them

16 releases

Uses old Rust 2015

0.4.1 Feb 27, 2020
0.4.0 Aug 17, 2018
0.3.3 Mar 9, 2017
0.2.2 Oct 7, 2016
0.1.2 Jul 27, 2015

⚠️ Issues reported

#849 in Data structures

Download history 25736/week @ 2024-10-30 17827/week @ 2024-11-06 52673/week @ 2024-11-13 31216/week @ 2024-11-20 23457/week @ 2024-11-27 32099/week @ 2024-12-04 39571/week @ 2024-12-11 14627/week @ 2024-12-18 5647/week @ 2024-12-25 16846/week @ 2025-01-01 31212/week @ 2025-01-08 33609/week @ 2025-01-15 19849/week @ 2025-01-22 36418/week @ 2025-01-29 64333/week @ 2025-02-05 22812/week @ 2025-02-12

150,127 downloads per month
Used in fewer than 71 crates

MIT license

68KB
1K SLoC

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

Dependencies

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy