1
1
use cpython:: { PyObject , PyErr , PyResult , PyTuple , ToPyObject , PythonObject } ;
2
- use lumol;
3
- use lumol:: types:: Vector3D ;
4
2
use std:: cell:: RefCell ;
3
+ use lumol;
4
+
5
+ use traits:: Callback ;
5
6
6
7
register ! ( |py, m| {
7
8
try!( m. add_class:: <Particle >( py) ) ;
8
9
Ok ( ( ) )
9
10
} ) ;
10
11
11
12
py_class ! ( class Particle |py| {
12
- data particle: RefCell < lumol:: sys:: Particle >;
13
+ data particle: Box < Callback < lumol:: sys:: Particle > >;
13
14
def __new__( _cls, name: & str ) -> PyResult <Particle > {
14
- Particle :: create_instance( py, RefCell :: new( lumol:: sys:: Particle :: new( name) ) )
15
+ Particle :: create_instance( py,
16
+ Box :: new( RefCell :: new( lumol:: sys:: Particle :: new( name) ) )
17
+ )
15
18
}
16
19
17
20
def name( & self ) -> PyResult <String > {
18
- Ok ( self . particle( py) . borrow( ) . name( ) . into( ) )
21
+ let mut name = String :: new( ) ;
22
+ self . particle( py) . with_ref( & mut |atom| {
23
+ name += atom. name( ) ;
24
+ } ) ;
25
+ Ok ( name)
19
26
}
20
27
21
28
def set_name( & self , name: & str ) -> PyResult <PyObject > {
22
- self . particle( py) . borrow_mut( ) . set_name( name) ;
29
+ self . particle( py) . with_mut( & mut |atom| {
30
+ atom. set_name( name)
31
+ } ) ;
23
32
Ok ( py. None ( ) )
24
33
}
25
34
26
35
def mass( & self ) -> PyResult <f64 > {
27
- Ok ( self . particle( py) . borrow( ) . mass)
36
+ let mut mass = 0.0 ;
37
+ self . particle( py) . with_ref( & mut |atom| {
38
+ mass = atom. mass;
39
+ } ) ;
40
+ Ok ( mass)
28
41
}
29
42
30
43
def set_mass( & self , mass: f64 ) -> PyResult <PyObject > {
31
- self . particle( py) . borrow_mut( ) . mass = mass;
44
+ self . particle( py) . with_mut( & mut |atom| {
45
+ atom. mass = mass;
46
+ } ) ;
32
47
Ok ( py. None ( ) )
33
48
}
34
49
35
50
def charge( & self ) -> PyResult <f64 > {
36
- Ok ( self . particle( py) . borrow( ) . charge)
51
+ let mut charge = 0.0 ;
52
+ self . particle( py) . with_ref( & mut |atom| {
53
+ charge = atom. charge;
54
+ } ) ;
55
+ Ok ( charge)
37
56
}
38
57
39
58
def set_charge( & self , charge: f64 ) -> PyResult <PyObject > {
40
- self . particle( py) . borrow_mut( ) . charge = charge;
59
+ self . particle( py) . with_mut( & mut |atom| {
60
+ atom. charge = charge;
61
+ } ) ;
41
62
Ok ( py. None ( ) )
42
63
}
43
64
44
65
def position( & self ) -> PyResult <PyTuple > {
45
- let position = & self . particle( py) . borrow( ) . position;
46
-
47
- let x = position[ 0 ] . to_py_object( py) . into_object( ) ;
48
- let y = position[ 1 ] . to_py_object( py) . into_object( ) ;
49
- let z = position[ 2 ] . to_py_object( py) . into_object( ) ;
50
-
51
- Ok ( PyTuple :: new( py, & [ x, y, z] ) )
66
+ let mut position = [ 0.0 ; 3 ] ;
67
+ self . particle( py) . with_ref( & mut |atom| {
68
+ position[ 0 ] = atom. position[ 0 ] ;
69
+ position[ 1 ] = atom. position[ 1 ] ;
70
+ position[ 2 ] = atom. position[ 2 ] ;
71
+ } ) ;
72
+ Ok ( PyTuple :: new( py, & [
73
+ position[ 0 ] . to_py_object( py) . into_object( ) ,
74
+ position[ 1 ] . to_py_object( py) . into_object( ) ,
75
+ position[ 2 ] . to_py_object( py) . into_object( ) ,
76
+ ] ) )
52
77
}
53
78
54
79
def set_position( & self , position: & PyTuple ) -> PyResult <PyObject > {
@@ -69,19 +94,26 @@ py_class!(class Particle |py| {
69
94
raise!( py, "Position elements should be numbers" ) )
70
95
) ;
71
96
72
- self . particle( py) . borrow_mut( ) . position = Vector3D :: new( x, y, z) ;
73
-
97
+ self . particle( py) . with_mut( & mut |atom| {
98
+ atom. position[ 0 ] = x;
99
+ atom. position[ 1 ] = y;
100
+ atom. position[ 2 ] = z;
101
+ } ) ;
74
102
Ok ( py. None ( ) )
75
103
}
76
104
77
105
def velocity( & self ) -> PyResult <PyTuple > {
78
- let velocity = & self . particle( py) . borrow( ) . velocity;
79
-
80
- let x = velocity[ 0 ] . to_py_object( py) . into_object( ) ;
81
- let y = velocity[ 1 ] . to_py_object( py) . into_object( ) ;
82
- let z = velocity[ 2 ] . to_py_object( py) . into_object( ) ;
83
-
84
- Ok ( PyTuple :: new( py, & [ x, y, z] ) )
106
+ let mut velocity = [ 0.0 ; 3 ] ;
107
+ self . particle( py) . with_ref( & mut |atom| {
108
+ velocity[ 0 ] = atom. velocity[ 0 ] ;
109
+ velocity[ 1 ] = atom. velocity[ 1 ] ;
110
+ velocity[ 2 ] = atom. velocity[ 2 ] ;
111
+ } ) ;
112
+ Ok ( PyTuple :: new( py, & [
113
+ velocity[ 0 ] . to_py_object( py) . into_object( ) ,
114
+ velocity[ 1 ] . to_py_object( py) . into_object( ) ,
115
+ velocity[ 2 ] . to_py_object( py) . into_object( ) ,
116
+ ] ) )
85
117
}
86
118
87
119
def set_velocity( & self , velocity: & PyTuple ) -> PyResult <PyObject > {
@@ -102,7 +134,11 @@ py_class!(class Particle |py| {
102
134
raise!( py, "Velocity elements should be numbers" ) )
103
135
) ;
104
136
105
- self . particle( py) . borrow_mut( ) . velocity = Vector3D :: new( x, y, z) ;
137
+ self . particle( py) . with_mut( & mut |atom| {
138
+ atom. velocity[ 0 ] = x;
139
+ atom. velocity[ 1 ] = y;
140
+ atom. velocity[ 2 ] = z;
141
+ } ) ;
106
142
107
143
Ok ( py. None ( ) )
108
144
}
0 commit comments