dyn_slice/
lib.rs

1//! An implementation for a `&dyn [Trait]`-like type, inspired by a [Reddit thread](https://www.reddit.com/r/rust/comments/14i08gz/dyn_slices).
2//!
3//! Indexing into a dyn slice yields a dyn object.
4//!
5//! # Examples
6//!
7//! ```
8#![doc = include_str!("../examples/display.rs")]
9//! ```
10//!
11#![doc = concat!("There are more examples in the [examples directory](https://docs.rs/crate/dyn-slice/", env!("CARGO_PKG_VERSION"), "/source/examples/).")]
12//!
13//! # Standard new dyn slice functions
14//!
15//! There are some pre-made new functions for common traits in [`standard`].
16
17#![feature(ptr_metadata, pointer_byte_offsets)]
18#![cfg_attr(doc, feature(doc_cfg))]
19#![warn(
20    clippy::all,
21    clippy::pedantic,
22    clippy::nursery,
23    clippy::perf,
24    clippy::cargo,
25    clippy::alloc_instead_of_core,
26    clippy::std_instead_of_alloc,
27    clippy::std_instead_of_core,
28    clippy::get_unwrap,
29    clippy::panic_in_result_fn,
30    clippy::pub_without_shorthand,
31    clippy::redundant_type_annotations,
32    clippy::todo,
33    clippy::undocumented_unsafe_blocks
34)]
35#![cfg_attr(not(feature = "std"), no_std)]
36
37#[cfg(test)]
38mod compile_tests;
39mod dyn_slice;
40mod dyn_slice_mut;
41mod iter;
42mod iter_mut;
43/// Dyn slice `new` and `new_mut` definitions for some common traits.
44///
45/// If you want a dyn slice for a trait that is not here, use the [`declare_new_fns`] macro.
46pub mod standard;
47
48pub use dyn_slice::*;
49pub use dyn_slice_mut::*;
50pub use iter::*;
51pub use iter_mut::*;
52
53/// Declare `new` and `new_mut` functions for dyn slices of a trait.
54///
55/// # Syntax
56/// ```text
57/// declare_new_fns!(
58///     #[attributes]
59///     pub name<parameters> Trait<arguments>
60///     where
61///         parameter: bounds,
62/// );
63/// ```
64///
65/// The [`ptr_metadata`](https://doc.rust-lang.org/beta/unstable-book/library-features/ptr-metadata.html)
66/// feature must be enabled to use this macro!
67///
68/// ## Example: Display
69/// ```
70/// #![feature(ptr_metadata)]
71/// # use dyn_slice::declare_new_fns;
72/// declare_new_fns!(
73///     display_slice std::fmt::Display
74/// );
75/// ```
76///
77/// ## Other examples
78#[doc = concat!("There are more examples of how to use [`declare_new_fns`] in the [examples directory](https://docs.rs/crate/dyn-slice/", env!("CARGO_PKG_VERSION"), "/source/examples/).")]
79///
80/// # Use from other crates
81/// When using `dyn_slice` from crates that re-export it, you may need to add a `crate` attribute, for example:
82/// ```text
83/// declare_new_fns!(
84///     #[crate = other_crate::dyn_slice]
85///     name Trait
86/// );
87/// ```
88pub use dyn_slice_macros::declare_new_fns;
89
90#[deprecated(
91    since = "3.2.0",
92    note = "this has been replaced with `declare_new_fns`. Convert to the new macro or expand this one"
93)]
94#[macro_export]
95/// DEPRECATED, use [`declare_new_fns`] instead!
96///
97/// Declare `new` and `new_mut` functions for dyn slices of a trait
98macro_rules! declare_new_fn {
99    (
100        $(#[ $meta:meta ])*
101        $(< $( $gen:ident ),* >,)?
102        $tr:path $( :<$( $trgen:ident ),*> )? $(:+ $atr:path )*,
103        $vis:vis $name:ident $(,)?
104    ) => {
105        $crate::declare_new_fns!(
106            $(#[ $meta ])*
107            $vis $name $(< $( $gen ),* >)?
108                $tr $(< $( $trgen ),* >)?
109        );
110    };
111}