Get the latest tech news
Sharing a mutable reference between Rust and Python
Background As part of my ongoing project to reimplement Django’s templating language in Rust, I have been adding support for custom template tags. Simple tags The simplest custom tag will look something like: # time_tags.py from datetime import datetime from django import template register = template.Library() @register.simple_tag def time(format_string): now = datetime.now() return now.strftime(format_string) # time_tags.py from datetime import datetime from django import template register = template.Library() @register.simple_tag def current_time(format_string): return datetime.now().strftime(format_string)
As part of my ongoing project to reimplement Django’s templating language in Rust, I have been adding support for . If the custom tag implementation keeps a reference around for some reason, we cannot take ownership. To do this, we can use a Mutex, along with PyO3’s MutexExt trait which provides the lock_py_attached method to avoid deadlocking with the Python interpreter:
Or read this on Hacker News