borrow-or-share

Traits for either borrowing or sharing data.

Latest version: 0.2.2 registry icon
Maintenance score
60
Safety score
100
Popularity score
70
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
0.2.2 0 0 0 0 0
0.2.1 0 0 0 0 0
0.2.0 0 0 0 0 0
0.1.0 0 0 0 0 0
0.0.1 0 0 0 0 0
0.0.0 0 0 0 0 0

Stability
Latest release:

0.2.2 - This version is safe to use because it has no known security vulnerabilities at this 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-0   -   MIT No Attribution

Not a wildcard

Not proprietary

OSI Compliant



borrow-or-share

Traits for either borrowing or sharing data.

crates.io build license

See below for a basic usage of the crate. See the documentation for a detailed walkthrough.

Basic usage

Suppose that you have a generic type that either owns some data or holds a reference to them. You can use this crate to implement on this type a method taking &self that either borrows from *self or from behind a reference it holds:

use borrow_or_share::BorrowOrShare;

struct Text<T>(T);

impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> Text<T> {
    fn as_str(&'i self) -> &'o str {
        self.0.borrow_or_share()
    }
}

// The returned reference is borrowed from `*text`
// and lives as long as `text`.
fn borrow(text: &Text<String>) -> &str {
    text.as_str()
}

// The returned reference is borrowed from `*text.0`, lives
// longer than `text` and is said to be shared with `*text`.
fn share<'a>(text: &Text<&'a str>) -> &'a str {
    text.as_str()
}

Credit

Credit goes to @beepster4096 for figuring out a safe version of the code.