Skip to content

Commit

Permalink
Added examples to the documentation of Dyn struct that show how to cr…
Browse files Browse the repository at this point in the history
…eate Dyn struct from:

- borrows of trait implementors
- heap allocated owned trait implementors
- thread safe owned trait implementors
  • Loading branch information
ZakharEl committed Mar 26, 2024
1 parent f51b60a commit 8da0647
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions stabby-abi/src/fatptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,41 @@ impl<'a, Vt: Copy + 'a> DynRef<'a, Vt> {
}
#[stabby::stabby]
/// A stable trait object (or a stable `&mut dyn`)
/// # Examples
/// ```rust
/// trait Piece {}
/// struct Slice;
/// impl Piece for Slice {}
/// struct Teaspoon;
/// impl Piece for Teaspoon {}
/// let slice = Slice;
/// let teaspoon = Teaspoon;
/// // Dyn<&'a mut (), vtable!(Piece)>
/// type DynPiece<'a> = stabby::dynptr!('a mut dyn Piece);

Check failure on line 165 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS

failed to resolve: use of undeclared crate or module `stabby`

Check failure on line 165 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-22.04

failed to resolve: use of undeclared crate or module `stabby`
/// // Dyn<&'a, stabby::boxed::Box<()>, vtable!(Piece)>
/// type DynBoxPiece<'a> = stabby::dynptr!(stabby::boxed::Box<dyn Piece + 'a>);

Check failure on line 167 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS

failed to resolve: use of undeclared crate or module `stabby`

Check failure on line 167 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-22.04

failed to resolve: use of undeclared crate or module `stabby`
/// // Dyn<&'a, stabby::sync::Arc<()>, vtable!(Piece)>
/// type DynArcPiece<'a> = stabby::dynptr!(stabby::sync::Arc<dyn Piece + 'a>);

Check failure on line 169 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS

failed to resolve: use of undeclared crate or module `stabby`

Check failure on line 169 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-22.04

failed to resolve: use of undeclared crate or module `stabby`
/// // Dyn and DynRef structs are constructed by using the from method of their From trait implementation
/// {
/// // Dyn struct construction from borrow of trait implementor
/// let dyn_piece = DynPiece::from(&slice);
/// let dyn_piece = DynPiece::from(&teaspoon);
/// }
/// // Dyn struct construction from heap allocated owned trait implementor
/// let inner_dyn_box_piece = stabby::boxed::Box::new(slice);

Check failure on line 177 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS

failed to resolve: use of undeclared crate or module `stabby`

Check failure on line 177 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-22.04

failed to resolve: use of undeclared crate or module `stabby`
/// let dyn_box_piece = DynBoxPiece::from(inner_dyn_box_piece);
/// let inner_dyn_box_piece = stabby::boxed::Box::new(teaspoon);

Check failure on line 179 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS

failed to resolve: use of undeclared crate or module `stabby`

Check failure on line 179 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-22.04

failed to resolve: use of undeclared crate or module `stabby`
/// let dyn_box_piece = DynBoxPiece::from(inner_dyn_box_piece);
/// // slice and teaspoon were previously moved so have to recreate them for DynArcPiece
/// let slice = Slice;
/// let teaspoon = Teaspoon;
/// // Dyn struct construction from thread safe owned trait implementor
/// let inner_dyn_arc_piece = stabby::sync::Arc::new(slice);

Check failure on line 185 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS

failed to resolve: use of undeclared crate or module `stabby`

Check failure on line 185 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-22.04

failed to resolve: use of undeclared crate or module `stabby`
/// let dyn_arc_piece = DynArcPiece::from(inner_dyn_arc_piece);
/// let inner_dyn_arc_piece = stabby::sync::Arc::new(teaspoon);

Check failure on line 187 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on macOS

failed to resolve: use of undeclared crate or module `stabby`

Check failure on line 187 in stabby-abi/src/fatptr.rs

View workflow job for this annotation

GitHub Actions / Run tests on ubuntu-22.04

failed to resolve: use of undeclared crate or module `stabby`
/// let dyn_arc_piece = DynArcPiece::from(inner_dyn_arc_piece);
/// ```
pub struct Dyn<'a, P: IPtrOwned + 'a, Vt: HasDropVt + 'static> {
ptr: core::mem::ManuallyDrop<P>,
vtable: &'static Vt,
Expand Down

0 comments on commit 8da0647

Please sign in to comment.