# Calling self functions in async

**URL:** <https://users.rust-lang.org/t/calling-self-functions-in-async/53970>\
**Category:** help\
**Created:** [January 10, 2021, 8:15pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970 "2021-01-10T20:15:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_notsokind](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dr_notsokind/32/14026_2.png) [@dr\_notsokind](https://users.rust-lang.org/u/dr_notsokind)\
**Post date:** [January 10, 2021, 8:15pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970/1 "2021-01-10T20:15:14Z")

</div>

Hi! So I'm learning how to deal with async tasks and try to understand how to make them work. So far most of the issues have been solved by wrapping variables in an Arc\<Mutex\> for spawning them in a separate thread. The problem comes when I try to call a method from `self` containing a generic.

So for example (this is a minimal example to reproduce my case, it's not the actual code I'm working in), let's say I have this struct (I'm currently using tokio, but I assume the issue is not specific to the library):

```
struct MyStruct<B> {
    other: B
}

impl<B: std::fmt::Display> MyStruct<B> {
    async fn print_b(&self) {
        println!("{}", self.other);
    }

    async fn call_async(&self) {
        tokio::spawn(async move {
            self.print_b().await;
        });
    }
}

```

I'm obviously getting the error:

```
future cannot be sent between threads safely
future created by async block is not `Send`

```

The error makes sense, as we don't have any way to know that `B` implements `Send`, and actually it doesn't have to. But I'm having a lot of trouble figuring out how should I solve this. So is there a way to solve this in a more or less elegant way?

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [January 10, 2021, 8:26pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970/2 "2021-01-10T20:26:05Z")

</div>

You can just add `+ Send` to the `impl` block. Of course, once you do that, you'll run into trouble with having that `tokio::spawn` be _inside_ the method. I have a draft of an article which you may find helpful, even if its still a draft: [Actors with Tokio](https://draft.ryhl.io/blog/actors-with-tokio/)

---

<div class="post-metadata">

**Author:** ![dr\_notsokind](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dr_notsokind/32/14026_2.png) [@dr\_notsokind](https://users.rust-lang.org/u/dr_notsokind)\
**Post date:** [January 10, 2021, 9:01pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970/3 "2021-01-10T21:01:36Z")

</div>

Thanks! Yeah I thought about adding `+ Send` to `B` but the problem then is that I'd have to manually implement `Send` for any struct I use, right?

I'll check your article now, thanks!

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [January 10, 2021, 9:37pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970/4 "2021-01-10T21:37:11Z")

</div>

The `Send` trait is an auto-trait, which means that it is automatically implemented for any type for which every field is `Send`.

---

<div class="post-metadata">

**Author:** ![dr\_notsokind](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dr_notsokind/32/14026_2.png) [@dr\_notsokind](https://users.rust-lang.org/u/dr_notsokind)\
**Post date:** [January 10, 2021, 9:42pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970/5 "2021-01-10T21:42:30Z")

</div>

Oh gotcha! Thanks!

One follow up question: I guess then there's no way to do it with a type that doesn't implement `Send` then

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [January 10, 2021, 10:34pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970/6 "2021-01-10T22:34:59Z")

</div>

No of course, if the type is not `Send`, you cannot send it to other threads. That's what `Send` means.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [April 10, 2021, 10:35pm UTC](https://users.rust-lang.org/t/calling-self-functions-in-async/53970/7 "2021-04-10T22:35:06Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
