Struct PythonOptions

Source
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

Source

pub fn with_tokio() -> Self

Create PythonOptions with tokio runtime (default)

Source

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}
Source

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}
Source

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}
Source

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

Source§

fn clone(&self) -> PythonOptions

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PythonOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PythonOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErrorContext for T
where T: Debug,

Source§

fn with_context(&self, operation: &str) -> String

Generate a standardized error message with context.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,