Skip to content

Commit a3471eb

Browse files
committed
fix formatting
1 parent 20b9242 commit a3471eb

9 files changed

Lines changed: 139 additions & 77 deletions

File tree

src/asyncio.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ pub(crate) fn copy_context(py: Python) -> Py<PyAny> {
3232
};
3333
ctx.unbind()
3434
}
35-

src/blocking.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crossbeam_channel as channel;
22
use pyo3::prelude::*;
33
use std::{
4-
sync::{Arc, atomic},
4+
sync::{atomic, Arc},
55
thread, time,
66
};
77

@@ -15,7 +15,9 @@ impl BlockingTask {
1515
where
1616
T: FnOnce(Python) + Send + 'static,
1717
{
18-
Self { inner: Box::new(inner) }
18+
Self {
19+
inner: Box::new(inner),
20+
}
1921
}
2022

2123
#[inline(always)]
@@ -113,7 +115,12 @@ impl BlockingRunnerPool {
113115
}
114116
if self
115117
.spawning
116-
.compare_exchange(false, true, atomic::Ordering::Relaxed, atomic::Ordering::Relaxed)
118+
.compare_exchange(
119+
false,
120+
true,
121+
atomic::Ordering::Relaxed,
122+
atomic::Ordering::Relaxed,
123+
)
117124
.is_err()
118125
{
119126
return;
@@ -128,8 +135,10 @@ impl BlockingRunnerPool {
128135
tcount.fetch_sub(1, atomic::Ordering::Release);
129136
});
130137

131-
self.spawn_tick
132-
.store(self.birth.elapsed().as_micros() as u64, atomic::Ordering::Relaxed);
138+
self.spawn_tick.store(
139+
self.birth.elapsed().as_micros() as u64,
140+
atomic::Ordering::Relaxed,
141+
);
133142
self.spawning.store(false, atomic::Ordering::Relaxed);
134143
}
135144

@@ -161,4 +170,3 @@ fn blocking_worker_idle(queue: channel::Receiver<BlockingTask>, timeout: time::D
161170
}
162171
});
163172
}
164-

src/callbacks.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use pyo3::{IntoPyObjectExt, exceptions::PyStopIteration, prelude::*};
2-
use std::sync::{Arc, OnceLock, RwLock, atomic};
1+
use pyo3::{exceptions::PyStopIteration, prelude::*, IntoPyObjectExt};
2+
use std::sync::{atomic, Arc, OnceLock, RwLock};
33
use tokio::sync::Notify;
44

55
use crate::conversion::FutureResultToPy;
@@ -91,7 +91,10 @@ impl PyIterAwaitable {
9191

9292
#[inline]
9393
pub(crate) fn set_result(pyself: Py<Self>, py: Python, result: FutureResultToPy) {
94-
_ = pyself.get().result.set(result.into_pyobject(py).map(Bound::unbind));
94+
_ = pyself
95+
.get()
96+
.result
97+
.set(result.into_pyobject(py).map(Bound::unbind));
9598
pyself.drop_ref(py);
9699
}
97100
}
@@ -157,7 +160,9 @@ impl PyFutureAwaitable {
157160
pub(crate) fn set_result(pyself: Py<Self>, py: Python, result: FutureResultToPy) {
158161
let rself = pyself.get();
159162

160-
_ = rself.result.set(result.into_pyobject(py).map(Bound::unbind));
163+
_ = rself
164+
.result
165+
.set(result.into_pyobject(py).map(Bound::unbind));
161166
if rself
162167
.state
163168
.compare_exchange(
@@ -226,7 +231,11 @@ impl PyFutureAwaitable {
226231
}
227232

228233
#[pyo3(signature = (cb, context=None))]
229-
fn add_done_callback(pyself: PyRef<'_, Self>, cb: Py<PyAny>, context: Option<Py<PyAny>>) -> PyResult<()> {
234+
fn add_done_callback(
235+
pyself: PyRef<'_, Self>,
236+
cb: Py<PyAny>,
237+
context: Option<Py<PyAny>>,
238+
) -> PyResult<()> {
230239
let py = pyself.py();
231240
let kwctx = pyo3::types::PyDict::new(py);
232241
kwctx.set_item(pyo3::intern!(py, "context"), context)?;
@@ -237,7 +246,12 @@ impl PyFutureAwaitable {
237246
*ack = Some((cb, kwctx.unbind()));
238247
} else {
239248
let event_loop = pyself.event_loop.clone_ref(py);
240-
event_loop.call_method(py, pyo3::intern!(py, "call_soon"), (cb, pyself), Some(&kwctx))?;
249+
event_loop.call_method(
250+
py,
251+
pyo3::intern!(py, "call_soon"),
252+
(cb, pyself),
253+
Some(&kwctx),
254+
)?;
241255
}
242256

243257
Ok(())
@@ -279,7 +293,12 @@ impl PyFutureAwaitable {
279293
let ctx = ctx.clone_ref(py);
280294
drop(ack);
281295

282-
let _ = event_loop.call_method(py, pyo3::intern!(py, "call_soon"), (cb, pyself), Some(ctx.bind(py)));
296+
let _ = event_loop.call_method(
297+
py,
298+
pyo3::intern!(py, "call_soon"),
299+
(cb, pyself),
300+
Some(ctx.bind(py)),
301+
);
283302
}
284303

285304
true
@@ -350,7 +369,13 @@ impl PyFutureDoneCallback {
350369
pub fn __call__(&self, fut: Bound<PyAny>) -> PyResult<()> {
351370
let py = fut.py();
352371

353-
if { fut.getattr(pyo3::intern!(py, "cancelled"))?.call0()?.is_truthy() }.unwrap_or(false) {
372+
if {
373+
fut.getattr(pyo3::intern!(py, "cancelled"))?
374+
.call0()?
375+
.is_truthy()
376+
}
377+
.unwrap_or(false)
378+
{
354379
self.cancel_tx.notify_one();
355380
}
356381

@@ -367,4 +392,3 @@ impl PyFutureResultSetter {
367392
let _ = target.call1((value,));
368393
}
369394
}
370-

src/conversion.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ impl<'p> IntoPyObject<'p> for FutureResultToPy {
1919
Self::Value(val) => {
2020
let bound = val.bind(py);
2121
Ok(bound.clone())
22-
},
22+
}
2323
}
2424
}
2525
}
26-

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod callbacks;
44
mod conversion;
55
mod executors;
66
mod io_helpers;
7-
mod runtime;
87
mod routers;
8+
mod runtime;
99
mod server;
1010
mod shared_socket;
1111
mod types;
@@ -65,7 +65,7 @@ pub fn robyn(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
6565
m.add_class::<callbacks::PyIterAwaitable>()?;
6666
m.add_class::<callbacks::PyFutureAwaitable>()?;
6767

68-
// Note: prepare_freethreaded_python is deprecated, but Python::initialize()
68+
// Note: prepare_freethreaded_python is deprecated, but Python::initialize()
6969
// is not available in pymodule context. This is safe to ignore for now.
7070
#[allow(deprecated)]
7171
pyo3::prepare_freethreaded_python();

src/routers/const_router.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use actix_http::StatusCode;
2-
use actix_web::{HttpResponse, HttpResponseBuilder, web::Bytes};
2+
use actix_web::{web::Bytes, HttpResponse, HttpResponseBuilder};
33
use parking_lot::RwLock;
44
use std::collections::HashMap;
55
use std::sync::Arc;
66

77
use crate::executors::execute_http_function;
88
use crate::types::function_info::FunctionInfo;
9+
use crate::types::headers::Headers;
910
use crate::types::request::Request;
1011
use crate::types::response::Response;
11-
use crate::types::headers::Headers;
1212
use crate::types::HttpMethod;
1313
use anyhow::Context;
1414
use matchit::Router as MatchItRouter;
@@ -92,7 +92,11 @@ impl Router<Response, HttpMethod> for ConstRouter {
9292
event_loop: Option<Bound<'py, pyo3::PyAny>>,
9393
) -> Result<(), Error> {
9494
let table = Arc::clone(self.routes.get(route_type).context("No relevant map")?);
95-
let fast_table = Arc::clone(self.fast_routes.get(route_type).context("No relevant fast map")?);
95+
let fast_table = Arc::clone(
96+
self.fast_routes
97+
.get(route_type)
98+
.context("No relevant fast map")?,
99+
);
96100

97101
let route = route.to_string();
98102
let event_loop =
@@ -131,14 +135,23 @@ impl ConstRouter {
131135
let mut routes = HashMap::new();
132136
let mut fast_routes = HashMap::new();
133137
for method in [
134-
HttpMethod::GET, HttpMethod::POST, HttpMethod::PUT,
135-
HttpMethod::DELETE, HttpMethod::PATCH, HttpMethod::HEAD,
136-
HttpMethod::OPTIONS, HttpMethod::CONNECT, HttpMethod::TRACE,
138+
HttpMethod::GET,
139+
HttpMethod::POST,
140+
HttpMethod::PUT,
141+
HttpMethod::DELETE,
142+
HttpMethod::PATCH,
143+
HttpMethod::HEAD,
144+
HttpMethod::OPTIONS,
145+
HttpMethod::CONNECT,
146+
HttpMethod::TRACE,
137147
] {
138148
routes.insert(method.clone(), Arc::new(RwLock::new(MatchItRouter::new())));
139149
fast_routes.insert(method, Arc::new(RwLock::new(HashMap::new())));
140150
}
141-
Self { routes, fast_routes }
151+
Self {
152+
routes,
153+
fast_routes,
154+
}
142155
}
143156

144157
/// Bake global response headers into all cached responses.

src/runtime.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
use pyo3::{IntoPyObjectExt, prelude::*};
2-
use std::{
3-
future::Future,
4-
sync::Arc,
5-
};
6-
use tokio::{
7-
runtime::Builder as RuntimeBuilder,
8-
task::JoinHandle,
9-
};
101
use futures::FutureExt;
2+
use pyo3::{prelude::*, IntoPyObjectExt};
3+
use std::{future::Future, sync::Arc};
4+
use tokio::{runtime::Builder as RuntimeBuilder, task::JoinHandle};
115

126
#[cfg(unix)]
137
use super::callbacks::PyFutureAwaitable;
@@ -74,7 +68,11 @@ impl RuntimeWrapper {
7468
}
7569

7670
pub fn handler(&self) -> RuntimeRef {
77-
RuntimeRef::new(self.inner.handle().clone(), self.br.clone(), self.pr.clone())
71+
RuntimeRef::new(
72+
self.inner.handle().clone(),
73+
self.br.clone(),
74+
self.pr.clone(),
75+
)
7876
}
7977
}
8078

@@ -86,7 +84,11 @@ pub struct RuntimeRef {
8684
}
8785

8886
impl RuntimeRef {
89-
pub fn new(rt: tokio::runtime::Handle, br: Arc<BlockingRunner>, pyloop: Arc<Py<PyAny>>) -> Self {
87+
pub fn new(
88+
rt: tokio::runtime::Handle,
89+
br: Arc<BlockingRunner>,
90+
pyloop: Arc<Py<PyAny>>,
91+
) -> Self {
9092
Self {
9193
inner: rt,
9294
innerb: br,
@@ -141,7 +143,10 @@ pub(crate) fn empty_future_into_py(py: Python) -> PyResult<Bound<PyAny>> {
141143
}
142144

143145
#[inline(always)]
144-
pub(crate) fn done_future_into_py(py: Python, result: PyResult<Py<PyAny>>) -> PyResult<Bound<PyAny>> {
146+
pub(crate) fn done_future_into_py(
147+
py: Python,
148+
result: PyResult<Py<PyAny>>,
149+
) -> PyResult<Bound<PyAny>> {
145150
PyDoneAwaitable::new(result).into_bound_py_any(py)
146151
}
147152

@@ -256,23 +261,28 @@ where
256261
// We have a handle, use our optimized implementation
257262
// Get the event loop from Python
258263
let asyncio = py.import("asyncio")?;
259-
let event_loop = asyncio.call_method0("get_event_loop")
264+
let event_loop = asyncio
265+
.call_method0("get_event_loop")
260266
.or_else(|_| asyncio.call_method0("new_event_loop"))?;
261267
let event_loop: Py<PyAny> = event_loop.unbind();
262268

263269
// Create a simple blocking runner (1 thread, 30s timeout)
264270
let blocking_runner = Arc::new(BlockingRunner::new(1, 30));
265-
271+
266272
// Create RuntimeRef
267273
let rt_ref = RuntimeRef::new(rt_handle, blocking_runner, Arc::new(event_loop));
268274

269275
// Convert the future to use FutureResultToPy
270276
let wrapped_fut = async move {
271277
match fut.await {
272278
Ok(()) => FutureResultToPy::None,
273-
Err(e) => FutureResultToPy::Err(Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
274-
format!("Future error: {}", e)
275-
))),
279+
Err(e) => FutureResultToPy::Err(Err(PyErr::new::<
280+
pyo3::exceptions::PyRuntimeError,
281+
_,
282+
>(format!(
283+
"Future error: {}",
284+
e
285+
)))),
276286
}
277287
};
278288

@@ -284,12 +294,14 @@ where
284294
// This happens when called from Python code that's not in a tokio async context
285295
// Convert Result<(), anyhow::Error> to PyResult<()> for pyo3_async_runtimes
286296
let py_fut = fut.map(|result| {
287-
result.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
288-
format!("Future error: {}", e)
289-
))
297+
result.map_err(|e| {
298+
PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
299+
"Future error: {}",
300+
e
301+
))
302+
})
290303
});
291304
pyo3_async_runtimes::tokio::future_into_py(py, py_fut)
292305
}
293306
}
294307
}
295-

0 commit comments

Comments
 (0)