Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow #[pyslot] for function pointer
  • Loading branch information
youknowone committed Dec 16, 2025
commit 73a8dd0202917ab938151bfb7f05ddd136e04ffb
6 changes: 4 additions & 2 deletions crates/derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ where
} else if let Ok(f) = args.item.function_or_method() {
(&f.sig().ident, f.span())
} else {
return Err(self.new_syn_error(args.item.span(), "can only be on a method"));
return Err(self.new_syn_error(args.item.span(), "can only be on a method or const function pointer"));
};

let item_attr = args.attrs.remove(self.index());
Expand Down Expand Up @@ -1496,7 +1496,9 @@ impl SlotItemMeta {
}
} else {
let ident_str = self.inner().item_name();
let name = if let Some(stripped) = ident_str.strip_prefix("slot_") {
// Convert to lowercase to handle both SLOT_NEW and slot_new
let ident_lower = ident_str.to_lowercase();
let name = if let Some(stripped) = ident_lower.strip_prefix("slot_") {
proc_macro2::Ident::new(stripped, inner.item_ident.span())
} else {
inner.item_ident.clone()
Expand Down
9 changes: 6 additions & 3 deletions crates/vm/src/types/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,13 +922,16 @@ pub trait DefaultConstructor: PyPayload + Default + std::fmt::Debug {
}
}

/// Slot function for types that cannot be instantiated through Python code.
pub fn unconstructible_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error(format!("cannot create '{}' instances", cls.slot_name())))
}

/// For types that cannot be instantiated through Python code.
#[pyclass]
pub trait Unconstructible: PyPayload {
#[pyslot]
fn slot_new(cls: PyTypeRef, _args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error(format!("cannot create '{}' instances", cls.slot_name())))
}
const SLOT_NEW: NewFunc = unconstructible_new;
}

impl<T> Constructor for T
Expand Down