@@ -25,7 +25,9 @@ instances of that Python class from Rust.
25
25
# Syntax
26
26
`py_class!(pub class MyType |py| { ... })`
27
27
28
- * `pub` makes the generated Rust struct visible outside the current module. It has no effect on the visibility from Python.
28
+ * `pub` makes the generated Rust struct visible outside the current module. It has no effect on
29
+ the visibility from Python. You may use any Rust visibility keyword. For example, `pub(crate)`
30
+ would also be valid.
29
31
* `MyType` is the name of the Python class.
30
32
* `py` is an identifier that will be made available as a variable of type `Python`
31
33
in all function bodies.
@@ -89,6 +91,7 @@ impl MyType {
89
91
* The generated type implements a number of traits from the `cpython` crate.
90
92
* The inherent `create_instance` method can create new Python objects
91
93
given the values for the data fields.
94
+ - Note: Any visibility keyword on the class will also be used for this method.
92
95
* Private accessors functions are created for the data fields.
93
96
* All functions callable from Python are also exposed as public Rust functions.
94
97
* To convert from `MyType` to `PyObject`, use `as_object()` or `into_object()` (from the `PythonObject` trait).
@@ -139,16 +142,21 @@ impl MyType {
139
142
140
143
## Instance methods
141
144
`def method_name(&self, parameter-list) -> PyResult<...> { ... }`
145
+ `pub(crate) def method_name(&self, parameter-list) -> PyResult<...> { ... }`
142
146
143
147
Declares an instance method callable from Python.
144
148
145
149
* Because Python objects are potentially shared, the self parameter must always
146
150
be a shared reference (`&self`).
147
151
* For details on `parameter-list`, see the documentation of `py_argparse!()`.
148
152
* The return type must be `PyResult<T>` for some `T` that implements `ToPyObject`.
153
+ * Visibility of the method in Rust defaults to `pub`. You may specify a visibility keyword
154
+ before the `def` to change the visibility, for example, to `pub(crate)`. Changing visibility
155
+ in Rust does not affect visibility in Python.
149
156
150
157
## Class methods
151
158
`@classmethod def method_name(cls, parameter-list) -> PyResult<...> { ... }`
159
+ `@classmethod pub(crate) def method_name(cls, parameter-list) -> PyResult<...> { ... }`
152
160
153
161
Declares a class method callable from Python.
154
162
@@ -157,14 +165,21 @@ Declares a class method callable from Python.
157
165
* The first parameter implicitly has type `&PyType`. This type must not be explicitly specified.
158
166
* For details on `parameter-list`, see the documentation of `py_argparse!()`.
159
167
* The return type must be `PyResult<T>` for some `T` that implements `ToPyObject`.
168
+ * Visibility of the method in Rust defaults to `pub`. You may specify a visibility keyword
169
+ before the `def` to change the visibility, for example, to `pub(crate)`. Changing visibility
170
+ in Rust does not affect visibility in Python.
160
171
161
172
## Static methods
162
173
`@staticmethod def method_name(parameter-list) -> PyResult<...> { ... }`
174
+ `@staticmethod pub(crate) def method_name(parameter-list) -> PyResult<...> { ... }`
163
175
164
176
Declares a static method callable from Python.
165
177
166
178
* For details on `parameter-list`, see the documentation of `py_argparse!()`.
167
179
* The return type must be `PyResult<T>` for some `T` that implements `ToPyObject`.
180
+ * Visibility of the method in Rust defaults to `pub`. You may specify a visibility keyword
181
+ before the `def` to change the visibility, for example, to `pub(crate)`. Changing visibility
182
+ in Rust does not affect visibility in Python.
168
183
169
184
## Properties
170
185
`@property def property_name(&self) -> PyResult<...> { ... }`
@@ -475,14 +490,14 @@ macro_rules! py_class {
475
490
/* props: */ { [ /* getters */ ] [ /* setters */ ] }
476
491
}
477
492
) ;
478
- ( pub class $class: ident |$py: ident| { $( $body: tt ) * } ) => (
493
+ ( $visibility : vis class $class: ident |$py: ident| { $( $body: tt ) * } ) => (
479
494
$crate:: py_class_impl! {
480
495
{ $( $body ) * }
481
496
$class $py
482
497
/* info: */ {
483
498
/* base_type: */ $crate:: PyObject ,
484
499
/* size: */ <$crate:: PyObject as $crate:: py_class:: BaseObject >:: size( ) ,
485
- /* class_visibility: */ { pub } ,
500
+ /* class_visibility: */ { $visibility } ,
486
501
/* gc: */ {
487
502
/* traverse_proc: */ None ,
488
503
/* traverse_data: */ [ /*name*/ ]
@@ -510,11 +525,11 @@ macro_rules! py_class {
510
525
#[ macro_export]
511
526
#[ doc( hidden) ]
512
527
macro_rules! py_class_impl_item {
513
- { $class: ident, $py: ident, $name: ident( $( $selfarg: tt ) * )
528
+ { $class: ident, $py: ident, $visibility : vis , $ name: ident( $( $selfarg: tt ) * )
514
529
$res_type: ty; $body: block [ $( { $pname: ident : $ptype: ty = $detail: tt } ) * ]
515
530
} => { $crate:: py_coerce_item! {
516
531
impl $class {
517
- pub fn $name( $( $selfarg ) * $py: $crate:: Python $( , $pname: $ptype ) * )
532
+ $visibility fn $name( $( $selfarg ) * $py: $crate:: Python $( , $pname: $ptype ) * )
518
533
-> $res_type $body
519
534
}
520
535
} }
0 commit comments