Skip to content

Commit d05645e

Browse files
committed
update syn-ext
1 parent 8ab4e77 commit d05645e

File tree

15 files changed

+82
-149
lines changed

15 files changed

+82
-149
lines changed

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ proc-macro = true
1212

1313
[dependencies]
1414
syn = { version = "1.0.91", features = ["full", "extra-traits"] }
15-
syn-ext = { version = "0.3.1", features = ["full"] }
15+
syn-ext = { version = "0.4.0", features = ["full"] }
1616
quote = "1.0.18"
1717
proc-macro2 = "1.0.37"
1818
rustpython-compiler = { path = "../compiler/porcelain", version = "0.1.1" }

derive/src/pyclass.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -548,25 +548,28 @@ where
548548
Item: ItemLike + ToTokens + GetIdent,
549549
{
550550
fn gen_impl_item(&self, args: ImplItemArgs<'_, Item>) -> Result<()> {
551-
let func = args
552-
.item
553-
.function_or_method()
554-
.map_err(|_| self.new_syn_error(args.item.span(), "can only be on a method"))?;
555-
let ident = &func.sig().ident;
551+
let (ident, span) = if let Ok(c) = args.item.constant() {
552+
(c.ident(), c.span())
553+
} else if let Ok(f) = args.item.function_or_method() {
554+
(&f.sig().ident, f.span())
555+
} else {
556+
return Err(self.new_syn_error(args.item.span(), "can only be on a method"));
557+
};
556558

557559
let item_attr = args.attrs.remove(self.index());
558560
let item_meta = SlotItemMeta::from_attr(ident.clone(), &item_attr)?;
559561

560562
let slot_ident = item_meta.slot_name()?;
563+
let slot_ident = Ident::new(&slot_ident.to_string().to_lowercase(), slot_ident.span());
561564
let slot_name = slot_ident.to_string();
562565
let tokens = {
563566
const NON_ATOMIC_SLOTS: &[&str] = &["as_buffer"];
564567
if NON_ATOMIC_SLOTS.contains(&slot_name.as_str()) {
565-
quote_spanned! { func.span() =>
568+
quote_spanned! { span =>
566569
slots.#slot_ident = Some(Self::#ident as _);
567570
}
568571
} else {
569-
quote_spanned! { func.span() =>
572+
quote_spanned! { span =>
570573
slots.#slot_ident.store(Some(Self::#ident as _));
571574
}
572575
}
@@ -599,11 +602,11 @@ where
599602
Ok(py_name)
600603
};
601604
let (ident, py_name, tokens) =
602-
if args.item.function_or_method().is_ok() || args.item.is_const() {
605+
if args.item.function_or_method().is_ok() || args.item.constant().is_ok() {
603606
let ident = args.item.get_ident().unwrap();
604607
let py_name = get_py_name(&attr, ident)?;
605608

606-
let value = if args.item.is_const() {
609+
let value = if args.item.constant().is_ok() {
607610
// TODO: ctx.new_value
608611
quote_spanned!(ident.span() => ctx.new_int(Self::#ident).into())
609612
} else {

stdlib/src/array.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,8 +1238,8 @@ mod array {
12381238
},
12391239
};
12401240

1241-
impl PyArray {
1242-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
1241+
impl AsMapping for PyArray {
1242+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
12431243
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
12441244
subscript: Some(|mapping, needle, vm| {
12451245
Self::mapping_downcast(mapping)._getitem(needle, vm)
@@ -1255,12 +1255,6 @@ mod array {
12551255
};
12561256
}
12571257

1258-
impl AsMapping for PyArray {
1259-
fn as_mapping(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
1260-
Self::MAPPING_METHODS
1261-
}
1262-
}
1263-
12641258
impl Iterable for PyArray {
12651259
fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
12661260
Ok(PyArrayIter {

vm/src/builtins/bytearray.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -690,23 +690,6 @@ impl PyByteArray {
690690
}
691691
}
692692

693-
impl PyByteArray {
694-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
695-
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
696-
subscript: Some(|mapping, needle, vm| {
697-
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
698-
}),
699-
ass_subscript: Some(|mapping, needle, value, vm| {
700-
let zelf = Self::mapping_downcast(mapping);
701-
if let Some(value) = value {
702-
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
703-
} else {
704-
zelf.delitem(needle.to_owned(), vm)
705-
}
706-
}),
707-
};
708-
}
709-
710693
impl Constructor for PyByteArray {
711694
type Args = FuncArgs;
712695

@@ -784,9 +767,20 @@ impl<'a> BufferResizeGuard<'a> for PyByteArray {
784767
}
785768

786769
impl AsMapping for PyByteArray {
787-
fn as_mapping(_zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
788-
Self::MAPPING_METHODS
789-
}
770+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
771+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
772+
subscript: Some(|mapping, needle, vm| {
773+
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
774+
}),
775+
ass_subscript: Some(|mapping, needle, value, vm| {
776+
let zelf = Self::mapping_downcast(mapping);
777+
if let Some(value) = value {
778+
Self::setitem(zelf.to_owned(), needle.to_owned(), value, vm)
779+
} else {
780+
zelf.delitem(needle.to_owned(), vm)
781+
}
782+
}),
783+
};
790784
}
791785

792786
impl AsSequence for PyByteArray {

vm/src/builtins/bytes.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,6 @@ impl PyBytes {
550550
}
551551
}
552552

553-
impl PyBytes {
554-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
555-
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
556-
subscript: Some(|mapping, needle, vm| Self::mapping_downcast(mapping)._getitem(needle, vm)),
557-
ass_subscript: None,
558-
};
559-
}
560-
561553
static BUFFER_METHODS: BufferMethods = BufferMethods {
562554
obj_bytes: |buffer| buffer.obj_as::<PyBytes>().as_bytes().into(),
563555
obj_bytes_mut: |_| panic!(),
@@ -577,9 +569,11 @@ impl AsBuffer for PyBytes {
577569
}
578570

579571
impl AsMapping for PyBytes {
580-
fn as_mapping(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
581-
Self::MAPPING_METHODS
582-
}
572+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
573+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
574+
subscript: Some(|mapping, needle, vm| Self::mapping_downcast(mapping)._getitem(needle, vm)),
575+
ass_subscript: None,
576+
};
583577
}
584578

585579
impl AsSequence for PyBytes {

vm/src/builtins/dict.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,6 @@ impl PyDict {
212212
pub fn size(&self) -> dictdatatype::DictSize {
213213
self.entries.size()
214214
}
215-
216-
pub(crate) const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
217-
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
218-
subscript: Some(|mapping, needle, vm| {
219-
Self::mapping_downcast(mapping).inner_getitem(needle, vm)
220-
}),
221-
ass_subscript: Some(|mapping, needle, value, vm| {
222-
let zelf = Self::mapping_downcast(mapping);
223-
if let Some(value) = value {
224-
zelf.inner_setitem(needle, value, vm)
225-
} else {
226-
zelf.inner_delitem(needle, vm)
227-
}
228-
}),
229-
};
230215
}
231216

232217
// Python dict methods:
@@ -476,9 +461,20 @@ impl Initializer for PyDict {
476461
}
477462

478463
impl AsMapping for PyDict {
479-
fn as_mapping(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
480-
Self::MAPPING_METHODS
481-
}
464+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
465+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
466+
subscript: Some(|mapping, needle, vm| {
467+
Self::mapping_downcast(mapping).inner_getitem(needle, vm)
468+
}),
469+
ass_subscript: Some(|mapping, needle, value, vm| {
470+
let zelf = Self::mapping_downcast(mapping);
471+
if let Some(value) = value {
472+
zelf.inner_setitem(needle, value, vm)
473+
} else {
474+
zelf.inner_delitem(needle, vm)
475+
}
476+
}),
477+
};
482478
}
483479

484480
impl AsSequence for PyDict {

vm/src/builtins/genericalias.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ pub fn subs_parameters<F: Fn(&VirtualMachine) -> PyResult<String>>(
306306
Ok(PyTuple::new_ref(new_args, &vm.ctx))
307307
}
308308

309-
impl PyGenericAlias {
310-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
309+
impl AsMapping for PyGenericAlias {
310+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
311311
length: None,
312312
subscript: Some(|mapping, needle, vm| {
313313
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
@@ -316,12 +316,6 @@ impl PyGenericAlias {
316316
};
317317
}
318318

319-
impl AsMapping for PyGenericAlias {
320-
fn as_mapping(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
321-
Self::MAPPING_METHODS
322-
}
323-
}
324-
325319
impl Callable for PyGenericAlias {
326320
type Args = FuncArgs;
327321
fn call(zelf: &crate::Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {

vm/src/builtins/list.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,6 @@ impl PyList {
352352
}
353353
}
354354

355-
impl PyList {
356-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
357-
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
358-
subscript: Some(|mapping, needle, vm| Self::mapping_downcast(mapping)._getitem(needle, vm)),
359-
ass_subscript: Some(|mapping, needle, value, vm| {
360-
let zelf = Self::mapping_downcast(mapping);
361-
if let Some(value) = value {
362-
zelf._setitem(needle, value, vm)
363-
} else {
364-
zelf._delitem(needle, vm)
365-
}
366-
}),
367-
};
368-
}
369-
370355
impl<'a> MutObjectSequenceOp<'a> for PyList {
371356
type Guard = PyMappedRwLockReadGuard<'a, [PyObjectRef]>;
372357

@@ -404,9 +389,18 @@ impl Initializer for PyList {
404389
}
405390

406391
impl AsMapping for PyList {
407-
fn as_mapping(_zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
408-
Self::MAPPING_METHODS
409-
}
392+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
393+
length: Some(|mapping, _vm| Ok(Self::mapping_downcast(mapping).len())),
394+
subscript: Some(|mapping, needle, vm| Self::mapping_downcast(mapping)._getitem(needle, vm)),
395+
ass_subscript: Some(|mapping, needle, value, vm| {
396+
let zelf = Self::mapping_downcast(mapping);
397+
if let Some(value) = value {
398+
zelf._setitem(needle, value, vm)
399+
} else {
400+
zelf._delitem(needle, vm)
401+
}
402+
}),
403+
};
410404
}
411405

412406
impl AsSequence for PyList {

vm/src/builtins/mappingproxy.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ impl PyMappingProxy {
157157
}
158158
}
159159

160-
impl PyMappingProxy {
161-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
160+
impl AsMapping for PyMappingProxy {
161+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
162162
length: None,
163163
subscript: Some(|mapping, needle, vm| {
164164
Self::mapping_downcast(mapping).getitem(needle.to_owned(), vm)
@@ -167,12 +167,6 @@ impl PyMappingProxy {
167167
};
168168
}
169169

170-
impl AsMapping for PyMappingProxy {
171-
fn as_mapping(_zelf: &crate::Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
172-
Self::MAPPING_METHODS
173-
}
174-
}
175-
176170
impl AsSequence for PyMappingProxy {
177171
fn as_sequence(
178172
_zelf: &crate::Py<Self>,

vm/src/builtins/memory.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,8 @@ impl Drop for PyMemoryView {
957957
}
958958
}
959959

960-
impl PyMemoryView {
961-
const MAPPING_METHODS: PyMappingMethods = PyMappingMethods {
960+
impl AsMapping for PyMemoryView {
961+
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
962962
length: Some(|mapping, vm| Self::mapping_downcast(mapping).len(vm)),
963963
subscript: Some(|mapping, needle, vm| {
964964
let zelf = Self::mapping_downcast(mapping);
@@ -975,12 +975,6 @@ impl PyMemoryView {
975975
};
976976
}
977977

978-
impl AsMapping for PyMemoryView {
979-
fn as_mapping(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyMappingMethods {
980-
Self::MAPPING_METHODS
981-
}
982-
}
983-
984978
impl AsSequence for PyMemoryView {
985979
fn as_sequence(_zelf: &Py<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
986980
Cow::Borrowed(&Self::SEQUENCE_METHODS)

0 commit comments

Comments
 (0)