-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.rs
66 lines (56 loc) · 1.64 KB
/
classes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::inspect::fields::FieldInfo;
/// Information about a Python class.
#[derive(Debug)]
pub struct ClassInfo<'a> {
/// Base information about the class.
pub class: &'a ClassStructInfo<'a>,
/// Information found in `#[pymethods]`.
pub fields: &'a [&'a FieldInfo<'a>],
}
/// Subset of available information about a Python class, including only what is available by parsing the `#[pyclass]`
/// block (methods are missing).
#[derive(Debug)]
pub struct ClassStructInfo<'a> {
pub name: &'a str,
pub base: Option<&'a str>,
pub fields: &'a [&'a FieldInfo<'a>],
}
impl<'a> ClassInfo<'a> {
/// The Python name of this class.
pub fn name(&'a self) -> &'a str {
self.class.name
}
/// The Python's base class.
pub fn base(&'a self) -> Option<&'a str> {
self.class.base
}
/// All fields of the class.
///
/// This includes:
/// - struct attributes annotated with `#[getter]` or `#[setter]`
/// - methods that appear in a `#[pymethods]` block
pub fn fields(&'a self) -> impl Iterator<Item=&'a &'a FieldInfo<'a>> + 'a {
self.class.fields
.iter()
.chain(self.fields)
}
}
pub trait InspectClass<'a> {
fn inspect() -> ClassInfo<'a>;
}
pub trait InspectStruct<'a> {
fn inspect_struct() -> &'a ClassStructInfo<'a>;
}
pub trait InspectImpl<'a> {
fn inspect_impl() -> &'a [&'a FieldInfo<'a>];
}
impl<'a, T> InspectClass<'a> for T
where T: InspectStruct<'a>, T: InspectImpl<'a>
{
fn inspect() -> ClassInfo<'a> {
ClassInfo {
class: Self::inspect_struct(),
fields: Self::inspect_impl(),
}
}
}