pub struct PythonOptions {
pub python_namespace: String,
pub python_path: Vec<String>,
pub imports: BTreeMap<String, HashSet<String>>,
pub scope: Scope,
pub stdpython: String,
pub with_std_python: bool,
pub allow_unsafe: bool,
pub async_runtime: AsyncRuntime,
}
Expand description
The global context for Python compilation.
Fields§
§python_namespace: String
Python imports are mapped into a given namespace that can be changed.
python_path: Vec<String>
The default path we will search for Python modules.
imports: BTreeMap<String, HashSet<String>>
Collects all of the things we need to compile imports[module][asnames]
scope: Scope
§stdpython: String
§with_std_python: bool
§allow_unsafe: bool
§async_runtime: AsyncRuntime
The async runtime to use for async Python code
Implementations§
Source§impl PythonOptions
impl PythonOptions
Sourcepub fn with_tokio() -> Self
pub fn with_tokio() -> Self
Create PythonOptions with tokio runtime (default)
Sourcepub fn with_async_std() -> Self
pub fn with_async_std() -> Self
Create PythonOptions with async-std runtime
Examples found in repository?
examples/async_runtime_example.rs (line 26)
3fn main() {
4 let python_code = r#"
5import asyncio
6
7async def fetch_data():
8 await asyncio.sleep(1)
9 return "data"
10
11async def main():
12 result = await fetch_data()
13 print(result)
14
15if __name__ == "__main__":
16 asyncio.run(main())
17"#;
18
19 // Example 1: Using tokio (default)
20 println!("=== Example 1: Tokio (default) ===");
21 let options = PythonOptions::default(); // tokio is the default
22 generate_and_display(python_code, options);
23
24 // Example 2: Using async-std
25 println!("\n=== Example 2: async-std ===");
26 let options = PythonOptions::with_async_std();
27 generate_and_display(python_code, options);
28
29 // Example 3: Using smol
30 println!("\n=== Example 3: smol ===");
31 let options = PythonOptions::with_smol();
32 generate_and_display(python_code, options);
33
34 // Example 4: Using custom runtime
35 println!("\n=== Example 4: Custom runtime ===");
36 let options = PythonOptions::with_custom_runtime("my_runtime::main", "my_runtime");
37 generate_and_display(python_code, options);
38
39 // Example 5: Programmatically setting runtime
40 println!("\n=== Example 5: Programmatic configuration ===");
41 let mut options = PythonOptions::default();
42 options.set_async_runtime(AsyncRuntime::AsyncStd);
43 generate_and_display(python_code, options);
44}
Sourcepub fn with_smol() -> Self
pub fn with_smol() -> Self
Create PythonOptions with smol runtime
Examples found in repository?
examples/async_runtime_example.rs (line 31)
3fn main() {
4 let python_code = r#"
5import asyncio
6
7async def fetch_data():
8 await asyncio.sleep(1)
9 return "data"
10
11async def main():
12 result = await fetch_data()
13 print(result)
14
15if __name__ == "__main__":
16 asyncio.run(main())
17"#;
18
19 // Example 1: Using tokio (default)
20 println!("=== Example 1: Tokio (default) ===");
21 let options = PythonOptions::default(); // tokio is the default
22 generate_and_display(python_code, options);
23
24 // Example 2: Using async-std
25 println!("\n=== Example 2: async-std ===");
26 let options = PythonOptions::with_async_std();
27 generate_and_display(python_code, options);
28
29 // Example 3: Using smol
30 println!("\n=== Example 3: smol ===");
31 let options = PythonOptions::with_smol();
32 generate_and_display(python_code, options);
33
34 // Example 4: Using custom runtime
35 println!("\n=== Example 4: Custom runtime ===");
36 let options = PythonOptions::with_custom_runtime("my_runtime::main", "my_runtime");
37 generate_and_display(python_code, options);
38
39 // Example 5: Programmatically setting runtime
40 println!("\n=== Example 5: Programmatic configuration ===");
41 let mut options = PythonOptions::default();
42 options.set_async_runtime(AsyncRuntime::AsyncStd);
43 generate_and_display(python_code, options);
44}
Sourcepub fn with_custom_runtime(
attribute: impl Into<String>,
import: impl Into<String>,
) -> Self
pub fn with_custom_runtime( attribute: impl Into<String>, import: impl Into<String>, ) -> Self
Create PythonOptions with a custom async runtime
Examples found in repository?
examples/async_runtime_example.rs (line 36)
3fn main() {
4 let python_code = r#"
5import asyncio
6
7async def fetch_data():
8 await asyncio.sleep(1)
9 return "data"
10
11async def main():
12 result = await fetch_data()
13 print(result)
14
15if __name__ == "__main__":
16 asyncio.run(main())
17"#;
18
19 // Example 1: Using tokio (default)
20 println!("=== Example 1: Tokio (default) ===");
21 let options = PythonOptions::default(); // tokio is the default
22 generate_and_display(python_code, options);
23
24 // Example 2: Using async-std
25 println!("\n=== Example 2: async-std ===");
26 let options = PythonOptions::with_async_std();
27 generate_and_display(python_code, options);
28
29 // Example 3: Using smol
30 println!("\n=== Example 3: smol ===");
31 let options = PythonOptions::with_smol();
32 generate_and_display(python_code, options);
33
34 // Example 4: Using custom runtime
35 println!("\n=== Example 4: Custom runtime ===");
36 let options = PythonOptions::with_custom_runtime("my_runtime::main", "my_runtime");
37 generate_and_display(python_code, options);
38
39 // Example 5: Programmatically setting runtime
40 println!("\n=== Example 5: Programmatic configuration ===");
41 let mut options = PythonOptions::default();
42 options.set_async_runtime(AsyncRuntime::AsyncStd);
43 generate_and_display(python_code, options);
44}
Sourcepub fn set_async_runtime(&mut self, runtime: AsyncRuntime) -> &mut Self
pub fn set_async_runtime(&mut self, runtime: AsyncRuntime) -> &mut Self
Set the async runtime for these options
Examples found in repository?
examples/async_runtime_example.rs (line 42)
3fn main() {
4 let python_code = r#"
5import asyncio
6
7async def fetch_data():
8 await asyncio.sleep(1)
9 return "data"
10
11async def main():
12 result = await fetch_data()
13 print(result)
14
15if __name__ == "__main__":
16 asyncio.run(main())
17"#;
18
19 // Example 1: Using tokio (default)
20 println!("=== Example 1: Tokio (default) ===");
21 let options = PythonOptions::default(); // tokio is the default
22 generate_and_display(python_code, options);
23
24 // Example 2: Using async-std
25 println!("\n=== Example 2: async-std ===");
26 let options = PythonOptions::with_async_std();
27 generate_and_display(python_code, options);
28
29 // Example 3: Using smol
30 println!("\n=== Example 3: smol ===");
31 let options = PythonOptions::with_smol();
32 generate_and_display(python_code, options);
33
34 // Example 4: Using custom runtime
35 println!("\n=== Example 4: Custom runtime ===");
36 let options = PythonOptions::with_custom_runtime("my_runtime::main", "my_runtime");
37 generate_and_display(python_code, options);
38
39 // Example 5: Programmatically setting runtime
40 println!("\n=== Example 5: Programmatic configuration ===");
41 let mut options = PythonOptions::default();
42 options.set_async_runtime(AsyncRuntime::AsyncStd);
43 generate_and_display(python_code, options);
44}
Trait Implementations§
Source§impl Clone for PythonOptions
impl Clone for PythonOptions
Source§fn clone(&self) -> PythonOptions
fn clone(&self) -> PythonOptions
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for PythonOptions
impl Debug for PythonOptions
Auto Trait Implementations§
impl Freeze for PythonOptions
impl RefUnwindSafe for PythonOptions
impl Send for PythonOptions
impl Sync for PythonOptions
impl Unpin for PythonOptions
impl UnwindSafe for PythonOptions
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ErrorContext for Twhere
T: Debug,
impl<T> ErrorContext for Twhere
T: Debug,
Source§fn with_context(&self, operation: &str) -> String
fn with_context(&self, operation: &str) -> String
Generate a standardized error message with context.