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:

Get the Android app

Or read this on Hacker News

Read more on:

Photo of python

python

Photo of Rust

Rust

Photo of mutable reference

mutable reference

Related news:

News photo

Show HN: LightCycle, a FOSS game in Rust based on Tron

News photo

Python has had async for 10 years – why isn't it more popular?

News photo

Rust 1.90 Switching To LLD Linker On Linux For Faster Linking Times