diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
commit | dc0db358abe19481e475e10c32149b53370f1a1c (patch) | |
tree | ab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/cxx/book/src/binding/sharedptr.md | |
parent | Releasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff) | |
download | rustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip |
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/cxx/book/src/binding/sharedptr.md')
-rw-r--r-- | vendor/cxx/book/src/binding/sharedptr.md | 80 |
1 files changed, 0 insertions, 80 deletions
diff --git a/vendor/cxx/book/src/binding/sharedptr.md b/vendor/cxx/book/src/binding/sharedptr.md deleted file mode 100644 index a3b707008..000000000 --- a/vendor/cxx/book/src/binding/sharedptr.md +++ /dev/null @@ -1,80 +0,0 @@ -{{#title std::shared_ptr<T> — Rust ♡ C++}} -# std::shared\_ptr\<T\> - -The Rust binding of std::shared\_ptr\<T\> is called **[`SharedPtr<T>`]**. See -the link for documentation of the Rust API. - -[`SharedPtr<T>`]: https://docs.rs/cxx/*/cxx/struct.SharedPtr.html - -### Restrictions: - -SharedPtr\<T\> does not support T being an opaque Rust type. You should use a -Box\<T\> (C++ [rust::Box\<T\>](box.md)) instead for transferring ownership of -opaque Rust types on the language boundary. - -## Example - -```rust,noplayground -// src/main.rs - -use std::ops::Deref; -use std::ptr; - -#[cxx::bridge] -mod ffi { - unsafe extern "C++" { - include!("example/include/example.h"); - - type Object; - - fn create_shared_ptr() -> SharedPtr<Object>; - } -} - -fn main() { - let ptr1 = ffi::create_shared_ptr(); - - { - // Create a second shared_ptr holding shared ownership of the same - // object. There is still only one Object but two SharedPtr<Object>. - // Both pointers point to the same object on the heap. - let ptr2 = ptr1.clone(); - assert!(ptr::eq(ptr1.deref(), ptr2.deref())); - - // ptr2 goes out of scope, but Object is not destroyed yet. - } - - println!("say goodbye to Object"); - - // ptr1 goes out of scope and Object is destroyed. -} -``` - -```cpp -// include/example.h - -#pragma once -#include <memory> - -class Object { -public: - Object(); - ~Object(); -}; - -std::shared_ptr<Object> create_shared_ptr(); -``` - -```cpp -// src/example.cc - -#include "example/include/example.h" -#include <iostream> - -Object::Object() { std::cout << "construct Object" << std::endl; } -Object::~Object() { std::cout << "~Object" << std::endl; } - -std::shared_ptr<Object> create_shared_ptr() { - return std::make_shared<Object>(); -} -``` |