i trying convert python script rust learning experience , make tool faster , shrink size of code/executable.
i'm trying convert section creates list of references methods on self. i've learned there isn't way bind self
variable method, 1 has use closure , close on object methods called on. when create closure gets assigned unique anonymous type, don't think can create vec
or array of closures without boxing them, might way go, has overhead might not necessary.
what i'm wondering is, instead of python informed design of list of method references, there more rusty way doesn't fight type system?
self.dataprocessors = [] if(self.datamode) : self.dataprocessors.append(self._processdata_) if(self.csvon): self.dataprocessors.append(self._processdata_csv_)
to honest, i’m not sure more rusty way, but, yes, can same way in python:
struct data; impl data { fn foo(&self) { println!("foo!"); } fn bar(&self) { println!("bar!"); } } fn main() { let x: vec<fn(&data) -> ()> = vec![data::foo, data::bar]; let data = data; f in x { f(&data); } }
note works in python: instead of keeping self
, explicitly pass first argument of now-function used method.
since there no need store values (which necessary if went closures), have function pointer (of type fn(&data) -> ()
, is, well, type of functions take data
, return unit), size known , don’t need boxing.
Comments
Post a Comment