File tree Expand file tree Collapse file tree 3 files changed +12
-14
lines changed
Expand file tree Collapse file tree 3 files changed +12
-14
lines changed Original file line number Diff line number Diff line change 11use crate :: db:: { Database , write} ;
2- use crate :: errors:: { Error , JsonErrorResponse } ;
2+ use crate :: errors:: JsonErrorResponse ;
33use crate :: id:: Id ;
44use axum:: Json ;
55use axum:: extract:: State ;
6- use rand:: Rng ;
76use serde:: { Deserialize , Serialize } ;
87use std:: num:: NonZeroU32 ;
98
@@ -40,11 +39,7 @@ pub async fn post(
4039 State ( db) : State < Database > ,
4140 Json ( entry) : Json < Entry > ,
4241) -> Result < Json < RedirectResponse > , JsonErrorResponse > {
43- let id: Id = tokio:: task:: spawn_blocking ( || rand:: rng ( ) . random :: < u32 > ( ) )
44- . await
45- . map_err ( Error :: from) ?
46- . into ( ) ;
47-
42+ let id = Id :: new ( ) ;
4843 let entry: write:: Entry = entry. into ( ) ;
4944 let path = format ! ( "/{}" , id. to_url_path( & entry) ) ;
5045 db. insert ( id, entry) . await ?;
Original file line number Diff line number Diff line change 1+ use crate :: Page ;
12use crate :: db:: { Database , write} ;
23use crate :: handlers:: extract:: { Theme , Uid } ;
34use crate :: handlers:: html:: make_error;
45use crate :: id:: Id ;
5- use crate :: { Error , Page } ;
66use axum:: extract:: { Form , State } ;
77use axum:: http:: HeaderMap ;
88use axum:: response:: { IntoResponse , Redirect } ;
99use axum_extra:: extract:: cookie:: { Cookie , SameSite , SignedCookieJar } ;
10- use rand:: Rng ;
1110use serde:: { Deserialize , Serialize } ;
1211use std:: num:: NonZeroU32 ;
1312
@@ -66,11 +65,6 @@ pub async fn post(
6665 . unwrap_or ( false ) ;
6766
6867 async {
69- let id: Id = tokio:: task:: spawn_blocking ( || rand:: rng ( ) . random :: < u32 > ( ) )
70- . await
71- . map_err ( Error :: from) ?
72- . into ( ) ;
73-
7468 // Use cookie uid or generate a new one.
7569 let uid = if let Some ( Uid ( uid) ) = uid {
7670 uid
@@ -81,6 +75,7 @@ pub async fn post(
8175 let mut entry: write:: Entry = entry. into ( ) ;
8276 entry. uid = Some ( uid) ;
8377
78+ let id = Id :: new ( ) ;
8479 let mut url = id. to_url_path ( & entry) ;
8580
8681 if entry. burn_after_reading . unwrap_or ( false ) {
Original file line number Diff line number Diff line change 11use crate :: db:: write:: Entry ;
22use crate :: errors:: Error ;
3+ use rand:: Rng ;
34use std:: fmt;
45use std:: str:: FromStr ;
56
@@ -17,6 +18,13 @@ pub(crate) struct Id {
1718}
1819
1920impl Id {
21+ /// Generate a new random [`Id`]. According to the [`rand::rng()`] documentation this should be
22+ /// fast and not require additional an `spawn_blocking()` call.
23+ pub fn new ( ) -> Self {
24+ let n = rand:: rng ( ) . random :: < u32 > ( ) ;
25+ Self { n }
26+ }
27+
2028 /// Return i64 representation for database storage purposes.
2129 pub fn to_i64 ( self ) -> i64 {
2230 self . n . into ( )
You can’t perform that action at this time.
0 commit comments