I have a struct containing a RefCell
wrapping a TlsStream<TcpStream>
. I tested replacing the TlsStream<IO>
with an i32
and am able to mutate the struct member, but the compiler errors when using the stream. I get the following error when attempting to write to it:
error[E0596]: cannot borrow data in a `&` reference as mutable
--> src/main.rs:18:9
|
18 | / self.i
19 | | .tcp4_stream
20 | | .borrow_mut()
21 | | .as_ref()
22 | | .unwrap()
| |_____________________^ cannot borrow as mutable
MCVE:
[package]
name = "mvce"
version = "0.1.0"
authors = ["Joshua Abraham <sinisterpatrician@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-std = "1.6.3"
async-tls = "0.9.0"
use async_std::net::TcpStream;
use async_std::prelude::*;
use async_tls::client::TlsStream;
use std::{cell::RefCell, rc::Rc};
#[derive(Default, Clone)]
struct Client {
i: Rc<ClientInternal>,
}
#[derive(Default)]
struct ClientInternal {
tcp4_stream: RefCell<Option<TlsStream<TcpStream>>>,
}
impl Client {
async fn send(&self) {
self.i
.tcp4_stream
.borrow_mut()
.as_ref()
.unwrap()
.write_all(b"hello world!")
.await;
}
}
fn main() {
println!("Hello, world!");
}
How can I make use of interior mutability here?