I'm using actix-web to serve a http post endpoint.
async fn index<'s, 'a: 's>(order: web::Json<match_orders::Order>, data: web::Data<match_orders::Container>) -> String {
let add_buy_order = data.add_buy_order(order.0);
match add_buy_order {
Some(true) => "Success".to_string(),
Some(false) => "-1".to_string(),
_ => "-2".to_string()
}
}
I get an error at data.add_buy_order(order) and that function corresponds to:
#[derive(Debug, Eq, PartialEq, Deserialize, Clone, Copy)]
pub struct Order {
pub amount_of_stock: i64,
pub value: NotNan<f64>,
pub stock_id: i64,
}
#[derive(Debug)]
pub struct Container {
pub buy_container: RwLock<BinaryHeap<Order>>,
pub sell_container: RwLock<BinaryHeap<Order>>,
pub(crate) stocks: RwLock<HashMap<i64, Stock>>,
}
impl<'a> Exchange<'a> for Container {
fn add_buy_order(&'a mut self, order: Order) -> Option<bool> {
match self.buy_container.get_mut() {
Ok(result) => result.push(order),
Err(e) => println!("error adding buy order in buy container: {:?}", e),
}
Some(true)
}
}
The error I get for the data.add_buy_order(order.0)
line is:
error: cannot borrow data in an `Arc` as mutable
error: cannot borrow as mutable
help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `Arc<Container>`
I suppose I don't really care if I borrow this - I need to create a copy anyway to store it in the heap.
I've tried implementing DerefMut and Deref for Container but it didn't seem to help (presumably because I need to implement it for Arc). I read online that by wrapping the Order in a mutex, I could resolve this issue but that gave me the same error.
However, I also read that Arc is not quite idiomatic rust (or rather it was a way of getting around the borrow checker). As a new rust user, I'd also like to know what the idiomatic way of solving this issue would look like.