1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*!
This crate provides traits and derive macros to change the lifetime of a type,
allowing efficient reuse of your stucts or enums with any lifetime requirement.

# `macros` feature

This crate's macros are not enabled by default.
Use the following in your `Cargo.toml`,
replacing `x.y` with the version you want,
to enable macros.
```toml
[dependencies]
lifetime = { version = "x.y", features = ["macros"] }
```

# no_std

Use the following in your `Cargo.toml`,
replacing `x.y` with the version you want,
to disable the default `std` and `alloc` features.
```toml
[dependencies]
lifetime = { version = "x.y", default-features = false }
```

*/
#![cfg_attr(
    feature = "alloc",
    doc = r##"

# Examples

Note that the examples use explicit types and lifetimes to be more illustrative.
Typically the lifetimes and/or types can be
[inferred](https://doc.rust-lang.org/rust-by-example/types/inference.html)
by the compiler.

```rust
use lifetime::{IntoStatic, ToBorrowed};
use std::borrow::Cow;

let alice: Cow<'static, str> = Cow::Borrowed("Alice");

let borrowed_alice: Cow<'_, str> = alice.to_borrowed();
assert_eq!(borrowed_alice, alice);

let static_alice: Cow<'static, str> = borrowed_alice.into_static();
assert_eq!(static_alice, alice);
```

"##
)]
#![cfg_attr(
    all(feature = "macros", feature = "alloc"),
    doc = r##"
Here's an example with a struct using the derive macros.
The `macros` feature needs to be enabled.
```rust
use lifetime::{IntoStatic, ToBorrowed};
use std::borrow::Cow;

#[derive(IntoStatic, ToBorrowed)]
struct Header<'a> {
    name: Cow<'a, str>,
    value: Cow<'a, str>,
}

let xml: Header<'static> = Header {
    name: "content".into(),
    value: "xml".into(),
};
let mut xml_or_json: Header<'_> = xml.to_borrowed();
xml_or_json.value += " or json"; // creates a new String

assert_eq!(xml.value, "xml"); // xml header has not moved
assert_eq!(xml_or_json.value, "xml or json");

// IntoStatic converts all borrowed `&str` into `String`
let static_xml_or_json: Header<'static> = xml_or_json.into_static();
assert_eq!(static_xml_or_json.value, "xml or json");
```
"##
)]
#![forbid(unsafe_code)]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

mod into_static;
mod to_borrowed;

pub use into_static::IntoStatic;
pub use to_borrowed::ToBorrowed;

#[cfg(feature = "macros")]
pub use lifetime_proc_macros::{IntoStatic, ToBorrowed};