#traits #stable #vec #deref #rc #marker #type

no-std stable_deref_trait

An unsafe marker trait for types like Box and Rc that dereference to a stable address even when moved, and hence can be used with libraries such as owning_ref and rental

4 stable releases

Uses old Rust 2015

1.2.0 Jul 13, 2020
1.1.1 Aug 2, 2018
1.1.0 Jun 9, 2018
1.0.0 Mar 9, 2017

#74 in Memory management

Download history 991198/week @ 2024-06-09 899259/week @ 2024-06-16 779232/week @ 2024-06-23 717201/week @ 2024-06-30 728409/week @ 2024-07-07 731202/week @ 2024-07-14 754776/week @ 2024-07-21 756217/week @ 2024-07-28 726561/week @ 2024-08-04 746204/week @ 2024-08-11 749618/week @ 2024-08-18 752209/week @ 2024-08-25 750508/week @ 2024-09-01 791677/week @ 2024-09-08 758255/week @ 2024-09-15 759991/week @ 2024-09-22

3,105,671 downloads per month
Used in 6,132 crates (90 directly)

MIT/Apache

12KB
56 lines

This crate defines an unsafe marker trait, StableDeref, for container types which deref to a fixed address which is valid even when the containing type is moved. For example, Box, Vec, Rc, Arc and String implement this trait. Additionally, it defines CloneStableDeref for types like Rc where clones deref to the same address.

It is intended to be used by crates such as owning_ref and rental, as well as library authors who wish to make their code interoperable with such crates. For example, if you write a custom Vec type, you can implement StableDeref, and then users will be able to use your custom Vec type together with owning_ref and rental.

no_std support can be enabled by disabling default features (specifically "std"). In this case, the trait will not be implemented for the std types mentioned above, but you can still use it for your own types.

Enable the "alloc" feature (with default-features disabled) to have this trait be implemented for the above types from the built-in alloc crate, specifically

  • alloc::boxed::Box
  • alloc::vec::Vec
  • alloc::rc::Rc
  • alloc::arc::Arc
  • alloc::string::String

For example, this crate can be built with alloc support via the following command: cargo build --no-default-features --features alloc

Or added as a Cargo.toml dependency as follows:

[dependencies.stable_deref_trait]
version = "<version>"
default-features = false
features = [ "alloc" ]

No runtime deps

Features