summaryrefslogtreecommitdiffstats
path: root/vendor/cxx/book/src/binding/sharedptr.md
blob: a3b7070082b9f778471a328d5f2b5c4342ce463f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{{#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>();
}
```