Debian Codegraph @

/

debian rust-bounded-static 0.8.0-8debian librust-bounded-static-dev debian librust-bounded-static-0-dev debian librust-bounded-static-0.8+ahash-dev debian librust-bounded-static-0.8+alloc-dev debian librust-bounded-static-0.8+chrono-clock-dev debian librust-bounded-static-0.8+collections-dev debian librust-bounded-static-0.8+default-dev debian librust-bounded-static-0.8+derive-dev debian librust-bounded-static-0.8+smallvec-dev debian librust-bounded-static-0.8+smartstring-dev debian librust-bounded-static-0.8+smol-str-dev debian librust-bounded-static-0.8+std-dev debian librust-bounded-static-0.8-dev debian librust-bounded-static-0.8.0-dev debian librust-bounded-static-derive-dev debian librust-bounded-static-derive-0-dev debian librust-bounded-static-derive-0.8+default-dev debian librust-bounded-static-derive-0.8-dev debian librust-bounded-static-derive-0.8.0-dev
2 more… cargo bounded-static-derive 0.8.0cargo bounded-static 0.8.0
debianinimakefilemarkdownrustrustrust-c-abishelltomlyaml 30 files · 795 symbols
View dependency graph

Depends on

  • dh-rust (dh-sequence-rust)
  • rust-ahash (librust-ahash-dev)
  • rust-chrono (librust-chrono-dev)
  • debhelper-compat, librust-proc-macro2-1+default-dev, librust-proc-macro2-dev, librust-quote-1+default-dev, librust-quote-dev, librust-smallvec-dev, librust-smartstring-dev, librust-smol-str-dev, librust-syn-2+default-dev, librust-syn-2+full-dev, librust-syn-dev
README (full)

ci Documentation Crate

Bounded Static

This crate defines the ToBoundedStatic and IntoBoundedStatic traits, the ToStatic macro and provides impls for common types. This crate has zero-dependencies, is no_std friendly and forbids unsafe code.

As described in the Common Rust Lifetime Misconceptions:

T: 'static should be read as "T is bounded by a 'static lifetime" not "T has a 'static lifetime".

The traits ToBoundedStatic and IntoBoundedStatic can be used to convert any suitable T and &T to an owned T such that T: 'static. Both traits define an associated type which is bounded by 'static and provide a method to convert to that bounded type.

The macros ToStatic can be used to automatically derive ToBoundedStatic and IntoBoundedStatic for any struct or enum that can be converted to a form that is bounded by 'static.

Refer to the crate documentation for details and examples.

FAQ

When is this useful?

This is useful for data structures which directly or indirectly contain Cow<T> types that must be supplied to a function which requires the 'static bound (i.e. std::thread::spawn):

#[derive(Debug, PartialEq, ToStatic)]
struct Foo<'a> {
    foo: Cow<'a, str>,
    bar: Vec<Bar<'a>>
}
#[derive(Debug, PartialEq, ToStatic)]
enum Bar<'a> {
    First,
    Second(Cow<'a, str>),
}

fn main() {
    let value = String::from("data");
    let foo = Foo {
        foo: Cow::from(&value),
        bar: vec![Bar::First, Bar::Second(Cow::from(&value))]
    };
    let foo_static = foo.into_static();
    std::thread::spawn(move || {
        assert_eq!(foo_static.foo, "data");
        assert_eq!(foo_static.bar, vec![Bar::First, Bar::Second("data".into())])
    }).join().unwrap();
}

How does this differ from the ToOwned trait?

The ToOwned trait defines an associated type Owned which is not bound by 'static and therefore the follow will not compile:

use std::borrow::Cow;

fn main() {
    #[derive(Clone)]
    struct Foo<'a> {
        foo: Cow<'a, str>,
    }

    fn ensure_static<T: 'static>(_: T) {}

    let s = String::from("data");
    let foo = Foo { foo: Cow::from(&s) };
    ensure_static(foo.to_owned())
}

Results in the following error:

error[E0597]: `s` does not live long enough
  --> src/lib.rs:12:36
   |
12 |     let foo = Foo { foo: Cow::from(&s) };
   |                          ----------^^-
   |                          |         |
   |                          |         borrowed value does not live long enough
   |                          argument requires that `s` is borrowed for `'static`
13 |     ensure_static(foo.to_owned())
14 | }
   | - `s` dropped here while still borrowed

Replacing Clone with ToStatic and using into_static() (or to_static() as needed) allows the example to compile:

use std::borrow::Cow;

fn main() {

    #[derive(ToStatic)]
    struct Foo<'a> {
        foo: Cow<'a, str>,
    }

    fn ensure_static<T: 'static>(_: T) {}

    let s = String::from("data");
    let foo = Foo { foo: Cow::from(&s) };
    ensure_static(foo.into_static())
}

License

bounded-static is distributed under the terms of the Apache License (Version 2.0).

See LICENSE for details.

Copyright 2022

dir .github/
dir bounded-static/
dir bounded-static-derive/
dir debian/
file .gitignore
file CHANGELOG.md
file Cargo.toml
file LICENSE
file README.md
file deny.toml