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

Function impl_pyclass_impl

crates/derive-impl/src/pyclass.rs:103–319  ·  view source on GitHub ↗
(attr: PunctuatedNestedMeta, item: Item)

Source from the content-addressed store, hash-verified

101}
102
103pub(crate) fn impl_pyclass_impl(attr: PunctuatedNestedMeta, item: Item) -> Result<TokenStream> {
104 let mut context = ImplContext::default();
105 let mut tokens = match item {
106 Item::Impl(mut imp) => {
107 extract_items_into_context(&mut context, imp.items.iter_mut());
108
109 let (impl_ty, payload_guess) = match imp.self_ty.as_ref() {
110 syn::Type::Path(syn::TypePath {
111 path: syn::Path { segments, .. },
112 ..
113 }) if segments.len() == 1 => {
114 let segment = &segments[0];
115 let payload_ty = if segment.ident == "Py" || segment.ident == "PyRef" {
116 match &segment.arguments {
117 syn::PathArguments::AngleBracketed(
118 syn::AngleBracketedGenericArguments { args, .. },
119 ) if args.len() == 1 => {
120 let arg = &args[0];
121 match arg {
122 syn::GenericArgument::Type(syn::Type::Path(
123 syn::TypePath {
124 path: syn::Path { segments, .. },
125 ..
126 },
127 )) if segments.len() == 1 => segments[0].ident.clone(),
128 _ => {
129 return Err(syn::Error::new_spanned(
130 segment,
131 "Py{Ref}<T> is expected but Py{Ref}<?> is found",
132 ));
133 }
134 }
135 }
136 _ => {
137 return Err(syn::Error::new_spanned(
138 segment,
139 "Py{Ref}<T> is expected but Py{Ref}? is found",
140 ));
141 }
142 }
143 } else {
144 if !matches!(segment.arguments, syn::PathArguments::None) {
145 return Err(syn::Error::new_spanned(
146 segment,
147 "PyImpl can only be implemented for Py{Ref}<T> or T",
148 ));
149 }
150 segment.ident.clone()
151 };
152 (segment.ident.clone(), payload_ty)
153 }
154 _ => {
155 return Err(syn::Error::new_spanned(
156 imp.self_ty,
157 "PyImpl can only be implemented for Py{Ref}<T> or T",
158 ));
159 }
160 };

Callers 1

pyclassFunction · 0.85

Calls 12

extract_impl_attrsFunction · 0.85
into_errorMethod · 0.80
ErrClass · 0.50
iter_mutMethod · 0.45
as_refMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45
validateMethod · 0.45
mapMethod · 0.45
extendMethod · 0.45
is_emptyMethod · 0.45

Tested by

no test coverage detected