dungeon_cell/lib.rs
1// Enable `std` only if the feature is enabled or in test or doc mode.
2#![cfg_attr(not(any(test, doc, feature = "std")), no_std)]
3// Feature needed for more friendly compile time errors.
4#![cfg_attr(has_feature_const_type_name, feature(const_type_name))]
5// This crate **doesn't** use generic_const_exprs for correctness.
6// This feature is only used to trigger the compile time errors earlier.
7#![cfg_attr(has_feature_generic_const_exprs, allow(incomplete_features))]
8#![cfg_attr(has_feature_generic_const_exprs, feature(generic_const_exprs))]
9// Feature allows for automatic implementation of the coercion helpers.
10#![cfg_attr(feature = "unsize", feature(unsize))]
11// Consider unsafe functions as a external constraint only.
12#![deny(unsafe_op_in_unsafe_fn)]
13// Enable some extra warnings.
14#![warn(
15 clippy::undocumented_unsafe_blocks,
16 clippy::default_union_representation
17)]
18
19//! Cell and Cell-like types that can store any type without dynamic memory.
20//!
21//! Currently only the [`DungeonCore`] primitive is implemented (unsized, cell, stack vec, ... are in progress).
22//!
23//! #### Example
24//! ```
25//! use dungeon_cell::{DungeonCore, layout_for};
26//!
27//! // a default DungeonCore can store any 'static type that fits in the layout
28//! let mut core = DungeonCore::<layout_for!(String)>::default();
29//!
30//! // we can store a i32 and get it back
31//! core.store(1234);
32//! assert_eq!(core.take(), Some(1234i32));
33//!
34//! // we can store a f64 and get it back
35//! core.store(1.234);
36//! assert_eq!(core.take(), Some(1.234f64));
37//!
38//! // lets get adventurous and store a String
39//! core.store(String::from("hello world"));
40//! assert_eq!(core.take(), Some(String::from("hello world")));
41//!
42//! // we can't take a type the core isn't storing
43//! core.store(1234);
44//! assert_eq!(core.take(), None::<f32>);
45//!
46//! // we can borrow both unique and shared
47//! core.store(1234);
48//! *core.borrow_mut::<i32>().unwrap() += 10;
49//! assert_eq!(core.borrow(), Some(&1244i32));
50//! ```
51//!
52//! # Features
53//!
54//! - `"alloc"` - Enable support for `alloc` types.
55//! - `"std"` - Enable support for `std` types. Also enables `"alloc"`.
56//! - `"unsize"` - Enable use of the nightly feature `unsize`. Requires a nightly compiler.
57//! - `"many_arg_fn"` - Enable implementations for functions with 11-30 arguments.
58//!
59//! # No-std Support
60//!
61//! This crate is `#![no_std]` by default, it can be used anywhere Rust can.
62//!
63//! # Minimum Supported Rust Version
64//!
65//! Requires Rust 1.64.0.
66//!
67//! This crate follows the ["Latest stable Rust" policy](https://gist.github.com/alexheretic/d1e98d8433b602e57f5d0a9637927e0c). The listed MSRV won't be changed unless needed.
68//! However, updating the MSRV anywhere up to the latest stable at time of release
69//! is allowed.
70
71#[cfg(all(feature = "alloc", not(feature = "std")))]
72extern crate alloc;
73// #[cfg(all(feature = "alloc", feature = "std"))]
74// use std as alloc;
75
76// mod cfg_if;
77pub mod lifetime_type_id;
78
79pub mod bound;
80pub mod layout;
81pub mod marker_traits;
82
83// mod const_transmute;
84// mod prison;
85pub mod vtable;
86
87pub mod align;
88mod core;
89
90pub mod buffer;
91pub mod compile_time;
92// mod unsize;
93// mod cell;
94// mod any;
95// mod cell;
96
97// pub use const_transmute::*;
98// pub use prison::*;
99
100pub use self::core::*;
101// pub use unsize::*;
102// pub use cell::*;
103// pub use any::*;
104
105/// Used by doctests to panic even if const asserts have been disabled.
106#[doc(hidden)]
107#[cfg(use_const_assert)]
108pub const HAS_CONST_PANIC: () = ();
109
110unsafe fn unreachable() -> ! {
111 #[cfg(debug_assertions)]
112 unreachable!();
113
114 #[cfg(not(debug_assertions))]
115 unsafe {
116 ::core::hint::unreachable_unchecked()
117 }
118}