-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweak-ref.el
45 lines (32 loc) · 1.34 KB
/
weak-ref.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
;;; weak-ref.el --- Weak references for Emacs Lisp -*- lexical-binding: t; -*-
;; This is free and unencumbered software released into the public domain.
;; Author: Christopher Wellons <wellons@nullprogram.com>
;; Homepage: https://github.com/skeeto/elisp-weak-ref
;; Package-Requires: ((emacs "24.3"))
;; Version: 2.0
;;; Commentary:
;; The Emacs Lisp environment supports weak references, but only for
;; hash table keys and values. This can be exploited to generalize
;; weak references into two convenient macros:
;; * `weak-ref' : create a weak reference to an object
;; * `weak-ref-deref' : access the object behind a weak reference
;; The weakness can be demonstrated like so:
;; (setq ref (weak-ref (list 1 2 3)))
;; (weak-ref-deref ref) ; => (1 2 3)
;; (garbage-collect)
;; (weak-ref-deref ref) ; => nil
;; See also: https://github.com/melpa/melpa/pull/6670
;;; Code:
(defsubst weak-ref (thing)
"Return a new weak reference to THING.
The referenced object will not be protected from garbage
collection by this reference."
(let ((ref (make-hash-table :size 1 :weakness t :test 'eq)))
(prog1 ref
(puthash t thing ref))))
(defsubst weak-ref-deref (ref)
"Return the object referenced by REF.
Return NIL if the object no longer exists (garbage collected)."
(gethash t ref))
(provide 'weak-ref)
;;; weak-ref.el ends here