Re: [rust-dev] How to implement a singleton ?
Hm, you make a good point: I should probably add a Share bound somwhere in there... Am 15.05.2014 13:22 schrieb "Simon Sapin" : > On 15/05/2014 08:59, Christophe Pedretti wrote: > >> I am trying to implement a Singleton (an object which instantiate only >> once, successive instantiations returning the object itself). >> Any tutorial for this ? any idea ? example ? best practice ? >> > > Kimundi published this macro to define lazily initialized statics, which > look to me equivalent to singletons although there is no "instanciation" > involved: > > https://gist.github.com/Kimundi/8782487 > http://www.reddit.com/r/rust/comments/1wvxcn/lazily_initialized_statics/ > > I believe this is only memory-safe with types that satisfy the `Share` > kind. > > http://static.rust-lang.org/doc/master/core/kinds/trait.Share.html > > -- > Simon Sapin > ___ > Rust-dev mailing list > Rust-dev@mozilla.org > https://mail.mozilla.org/listinfo/rust-dev > ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] How to implement a singleton ?
On 15/05/2014 08:59, Christophe Pedretti wrote: I am trying to implement a Singleton (an object which instantiate only once, successive instantiations returning the object itself). Any tutorial for this ? any idea ? example ? best practice ? Kimundi published this macro to define lazily initialized statics, which look to me equivalent to singletons although there is no "instanciation" involved: https://gist.github.com/Kimundi/8782487 http://www.reddit.com/r/rust/comments/1wvxcn/lazily_initialized_statics/ I believe this is only memory-safe with types that satisfy the `Share` kind. http://static.rust-lang.org/doc/master/core/kinds/trait.Share.html -- Simon Sapin ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] How to implement a singleton ?
Hello, My first instinct would be: don't... but in the name of science... Have you tried looking at Stack Overflow ? Just googling around I found: http://stackoverflow.com/questions/19605132/is-it-possible-to-use-global-variables-in-rustwhich allows you to have a global variable and from there a Singleton seems easy. I guess you will need something like Mutex> if you want lazy initialization. -- Matthieu On Thu, May 15, 2014 at 9:59 AM, Christophe Pedretti < christophe.pedre...@gmail.com> wrote: > I am trying to implement a Singleton (an object which instantiate only > once, successive instantiations returning the object itself). > Any tutorial for this ? any idea ? example ? best practice ? > > thanks > ___ > Rust-dev mailing list > Rust-dev@mozilla.org > https://mail.mozilla.org/listinfo/rust-dev > ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev