MCPcopy Index your code
hub / github.com/RustPython/RustPython / generate_class_def

Function generate_class_def

crates/derive-impl/src/pyclass.rs:400–546  ·  view source on GitHub ↗
(
    ident: &Ident,
    name: &str,
    module_name: Option<&str>,
    base: Option<syn::Path>,
    metaclass: Option<String>,
    unhashable: bool,
    attrs: &[Attribute],
)

Source from the content-addressed store, hash-verified

398}
399
400fn generate_class_def(
401 ident: &Ident,
402 name: &str,
403 module_name: Option<&str>,
404 base: Option<syn::Path>,
405 metaclass: Option<String>,
406 unhashable: bool,
407 attrs: &[Attribute],
408) -> Result<TokenStream> {
409 let doc = attrs.doc().or_else(|| {
410 let module_name = module_name.unwrap_or("builtins");
411 DB.get(&format!("{module_name}.{name}"))
412 .copied()
413 .map(str::to_owned)
414 });
415 let doc = if let Some(doc) = doc {
416 quote!(Some(#doc))
417 } else {
418 quote!(None)
419 };
420 let module_class_name = if let Some(module_name) = module_name {
421 format!("{module_name}.{name}")
422 } else {
423 name.to_owned()
424 };
425 let module_name = match module_name {
426 Some(v) => quote!(Some(#v) ),
427 None => quote!(None),
428 };
429 let unhashable = if unhashable {
430 quote!(true)
431 } else {
432 quote!(false)
433 };
434 let is_pystruct = attrs.iter().any(|attr| {
435 attr.path().is_ident("derive")
436 && if let Ok(Meta::List(l)) = attr.parse_meta() {
437 l.nested
438 .into_iter()
439 .any(|n| n.get_ident().is_some_and(|p| p == "PyStructSequence"))
440 } else {
441 false
442 }
443 });
444 // Check if the type has #[repr(transparent)] - only then we can safely
445 // generate PySubclass impl (requires same memory layout as base type)
446 let is_repr_transparent = attrs.iter().any(|attr| {
447 attr.path().is_ident("repr")
448 && if let Ok(Meta::List(l)) = attr.parse_meta() {
449 l.nested
450 .into_iter()
451 .any(|n| n.get_ident().is_some_and(|p| p == "transparent"))
452 } else {
453 false
454 }
455 });
456 // If repr(transparent) with a base, the type has the same memory layout as base,
457 // so basicsize should be 0 (no additional space beyond the base type)

Callers 1

impl_pyclassFunction · 0.85

Calls 11

newFunction · 0.85
docMethod · 0.80
spanMethod · 0.80
SomeClass · 0.50
mapMethod · 0.45
getMethod · 0.45
to_ownedMethod · 0.45
iterMethod · 0.45
pathMethod · 0.45
into_iterMethod · 0.45
as_refMethod · 0.45

Tested by

no test coverage detected