|
@@ -1,4 +1,4 @@
|
|
|
-use std::{borrow::Cow, cell::RefCell};
|
|
|
|
|
|
|
+use std::cell::RefCell;
|
|
|
|
|
|
|
|
use nanorand::{Rng, WyRand};
|
|
use nanorand::{Rng, WyRand};
|
|
|
use ntex::util::{Bytes, BytesMut};
|
|
use ntex::util::{Bytes, BytesMut};
|
|
@@ -15,15 +15,15 @@ pub struct World {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, sonic_rs::Serialize)]
|
|
#[derive(Debug, sonic_rs::Serialize)]
|
|
|
-pub struct Fortune {
|
|
|
|
|
|
|
+pub struct Fortune<'a> {
|
|
|
pub id: i32,
|
|
pub id: i32,
|
|
|
- pub message: Cow<'static, str>,
|
|
|
|
|
|
|
+ pub message: &'a str,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[derive(yarte::TemplateBytes)]
|
|
#[derive(yarte::TemplateBytes)]
|
|
|
#[template(path = "fortune.hbs")]
|
|
#[template(path = "fortune.hbs")]
|
|
|
-pub struct FortunesTemplate<'a> {
|
|
|
|
|
- pub fortunes: &'a Vec<Fortune>,
|
|
|
|
|
|
|
+pub struct FortunesTemplate<'a, 'b> {
|
|
|
|
|
+ pub fortunes: &'a [Fortune<'b>],
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Postgres interface
|
|
/// Postgres interface
|
|
@@ -34,7 +34,6 @@ pub struct PgConnection {
|
|
|
rng: WyRand,
|
|
rng: WyRand,
|
|
|
updates: Statement,
|
|
updates: Statement,
|
|
|
buf: RefCell<BytesMut>,
|
|
buf: RefCell<BytesMut>,
|
|
|
- fbuf: RefCell<Vec<Fortune>>,
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
impl PgConnection {
|
|
impl PgConnection {
|
|
@@ -57,7 +56,6 @@ impl PgConnection {
|
|
|
updates,
|
|
updates,
|
|
|
rng: WyRand::new(),
|
|
rng: WyRand::new(),
|
|
|
buf: RefCell::new(BytesMut::with_capacity(10 * 1024 * 1024)),
|
|
buf: RefCell::new(BytesMut::with_capacity(10 * 1024 * 1024)),
|
|
|
- fbuf: RefCell::new(Vec::with_capacity(64)),
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -125,11 +123,13 @@ impl PgConnection {
|
|
|
});
|
|
});
|
|
|
queries.push(self.cl.query_one(&self.world, &[&ids[idx]]));
|
|
queries.push(self.cl.query_one(&self.world, &[&ids[idx]]));
|
|
|
});
|
|
});
|
|
|
- let _ = self
|
|
|
|
|
- .cl
|
|
|
|
|
- .query(&self.updates, &[&ids, &numbers])
|
|
|
|
|
- .await
|
|
|
|
|
- .unwrap();
|
|
|
|
|
|
|
+ let update = self.cl.query(&self.updates, &[&ids, &numbers]);
|
|
|
|
|
+
|
|
|
|
|
+ for query in queries {
|
|
|
|
|
+ let _rand: i32 = query.await.unwrap().get(1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ update.await.unwrap();
|
|
|
|
|
|
|
|
let mut body = self.buf.borrow_mut();
|
|
let mut body = self.buf.borrow_mut();
|
|
|
utils::reserve(&mut body, 2 * 1024);
|
|
utils::reserve(&mut body, 2 * 1024);
|
|
@@ -140,14 +140,14 @@ impl PgConnection {
|
|
|
pub async fn tell_fortune(&self) -> Bytes {
|
|
pub async fn tell_fortune(&self) -> Bytes {
|
|
|
let rows = self.cl.query_raw(&self.fortune, &[]).await.unwrap();
|
|
let rows = self.cl.query_raw(&self.fortune, &[]).await.unwrap();
|
|
|
|
|
|
|
|
- let mut fortunes = self.fbuf.borrow_mut();
|
|
|
|
|
|
|
+ let mut fortunes = Vec::with_capacity(16);
|
|
|
fortunes.push(Fortune {
|
|
fortunes.push(Fortune {
|
|
|
id: 0,
|
|
id: 0,
|
|
|
- message: Cow::Borrowed("Additional fortune added at request time."),
|
|
|
|
|
|
|
+ message: "Additional fortune added at request time.",
|
|
|
});
|
|
});
|
|
|
fortunes.extend(rows.iter().map(|row| Fortune {
|
|
fortunes.extend(rows.iter().map(|row| Fortune {
|
|
|
id: row.get(0),
|
|
id: row.get(0),
|
|
|
- message: Cow::Owned(row.get(1)),
|
|
|
|
|
|
|
+ message: row.get(1),
|
|
|
}));
|
|
}));
|
|
|
fortunes.sort_by(|it, next| it.message.cmp(&next.message));
|
|
fortunes.sort_by(|it, next| it.message.cmp(&next.message));
|
|
|
|
|
|