Skip to main content

FunctionDef

Struct FunctionDef 

Source
pub struct FunctionDef {
    pub name: String,
    pub args: ParameterList,
    pub body: Vec<Statement>,
    pub decorator_list: Vec<ExprType>,
    pub returns: Option<Box<ExprType>>,
}

Fields§

§name: String§args: ParameterList§body: Vec<Statement>§decorator_list: Vec<ExprType>§returns: Option<Box<ExprType>>

The function’s return annotation (-> int), if present.

Implementations§

Source§

impl FunctionDef

Source

pub fn resolved_return_type(&self) -> Option<TokenStream>

The return type the generated Rust function actually carries, if any.

Inference from the body comes first (it reflects the type the body actually produces — e.g. a string literal is a &’static str even under a -> str annotation); an explicit annotation with a known Rust mapping is the fallback for bodies inference can’t see through. Both require the body to return on every path: a fall-through path yields (), which no concrete annotation can type. -> None and unmappable annotations yield None.

Tools generating call-through code (e.g. PyO3 wrappers) must use this same method so their signatures match the generated function.

Source

pub fn ignored_return_annotation(&self) -> Option<String>

The Python-source text of a return annotation the generated function does not honor: the body can fall through (implicitly returning None), so the generated function returns () no matter what the annotation claims. This frequently marks a bug in the Python source — the author declared a return type but not every path returns one — so it must be surfaced, not silently reproduced.

Source

pub fn lossy_conversion_notes(&self) -> Vec<String>

Human-readable notes for every lossy conversion this function’s signature underwent. These become the #[deprecated] note on the generated function, and conversion tools report them to the user.

Source

pub fn dropped_default_parameters(&self) -> Vec<String>

Names of parameters whose Python default values cannot be carried into the generated Rust signature (Rust has no default arguments). Used to attach a call-site warning to the generated function and to let tools report the loss during conversion.

Source

pub fn inferred_return_type(&self) -> Option<TokenStream>

Infer a return type when the function is guaranteed to return on every control-flow path AND every return value in the body maps to the same simple type — either directly (a constant or f-string) or via a local variable assigned a constant. Partial/conditional returns (which implicitly return None on the fall-through path), mixed types, and uninferable values all yield None so the function stays unannotated, as before.

Trait Implementations§

Source§

impl Clone for FunctionDef

Source§

fn clone(&self) -> FunctionDef

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl CodeGen for FunctionDef

Source§

type Context = CodeGenContext

A type, generally an enum, that passes the code generator the context of the node.
Source§

type Options = PythonOptions

A struct representing the set of compilation options.
Source§

type SymbolTable = SymbolTableScopes

A trait for a symbol table
Source§

fn find_symbols(self, symbols: Self::SymbolTable) -> Self::SymbolTable

A default implementation for find_symbols(), which simply returns the input. Language nodes that modify the symbol table should override this method.
Source§

fn to_rust( self, ctx: Self::Context, options: Self::Options, symbols: SymbolTableScopes, ) -> Result<TokenStream, Box<dyn Error>>

A trait method to output Rust code in a general sense. The output should be stream of Rust tokens, however, it is not guaranteed that it will fully compile because of scoping errors and other checks that don’t occur until later.
Source§

fn get_docstring(&self) -> Option<String>

A trait method for extracting a docstring from an object that can have a docstring.
Source§

impl Debug for FunctionDef

Source§

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

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

impl<'de> Deserialize<'de> for FunctionDef

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, 'py> FromPyObject<'a, 'py> for FunctionDef

Source§

type Error = PyErr

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

fn extract(ob: Borrowed<'a, 'py, PyAny>) -> PyResult<Self>

Extracts Self from the bound smart pointer obj. Read more
Source§

impl Object for FunctionDef

Source§

fn id(&self) -> usize

Returns the unique identifier of the object, which is the memory address of the object.
Source§

fn type(&self) -> String

Returns the type name of the object.
Source§

fn is<T: Object>(&self, other: &Option<T>) -> bool

Returns the type name of the object.
Source§

fn __getattribute__(&self, _name: impl AsRef<str>) -> Option<impl Object>

getattribute is called to look up an attribute of the object.
Source§

fn __dir__(&self) -> Vec<impl AsRef<str>>

dir is called to list the attributes of the object.
Source§

impl PartialEq for FunctionDef

Source§

fn eq(&self, other: &FunctionDef) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PyStatementTrait for FunctionDef

Source§

impl Serialize for FunctionDef

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for FunctionDef

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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<'a, T> ExtractFromPython<'a> for T
where T: FromPyObjectOwned<'a>,

Source§

fn extract_with_context( ob: &Bound<'a, PyAny>, context: &str, ) -> Result<T, PyErr>

Extract from Python object with context for better error messages.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<'py, T> FromPyObjectOwned<'py> for T
where T: for<'a> FromPyObject<'a, 'py>,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more