- (elt inst (cl-struct-slot-offset struct-type slot-name)))
-(put 'cl-struct-slot-value 'side-effect-free t)
-
-(defun cl-struct-set-slot-value (struct-type slot-name inst value)
- "Set the value of slot SLOT-NAME in INST of STRUCT-TYPE.
-STRUCT and SLOT-NAME are symbols. INST is a structure instance.
-VALUE is the value to which to set the given slot. Return
-VALUE."
- (unless (cl-typep inst struct-type)
- (signal 'wrong-type-argument (list struct-type inst)))
- (setf (elt inst (cl-struct-slot-offset struct-type slot-name)) value))
-
-(gv-define-simple-setter cl-struct-slot-value cl-struct-set-slot-value)
-
-(cl-define-compiler-macro cl-struct-slot-value
- (&whole orig struct-type slot-name inst)
- (or (let* ((macenv macroexpand-all-environment)
- (struct-type (cl--const-expr-val struct-type macenv))
- (slot-name (cl--const-expr-val slot-name macenv)))
- (and struct-type (symbolp struct-type)
- slot-name (symbolp slot-name)
- (assq slot-name (cl-struct-slot-info struct-type))
- (let ((idx (cl-struct-slot-offset struct-type slot-name)))
- (cl-ecase (cl-struct-sequence-type struct-type)
- (vector `(aref (cl-the ,struct-type ,inst) ,idx))
- (list `(nth ,idx (cl-the ,struct-type ,inst)))))))
- orig))
-
-(cl-define-compiler-macro cl-struct-set-slot-value
- (&whole orig struct-type slot-name inst value)
- (or (let* ((macenv macroexpand-all-environment)
- (struct-type (cl--const-expr-val struct-type macenv))
- (slot-name (cl--const-expr-val slot-name macenv)))
- (and struct-type (symbolp struct-type)
- slot-name (symbolp slot-name)
- (assq slot-name (cl-struct-slot-info struct-type))
- (let ((idx (cl-struct-slot-offset struct-type slot-name)))
- (cl-ecase (cl-struct-sequence-type struct-type)
- (vector `(setf (aref (cl-the ,struct-type ,inst) ,idx)
- ,value))
- (list `(setf (nth ,idx (cl-the ,struct-type ,inst))
- ,value))))))
- orig))
+ ;; We could use `elt', but since the byte compiler will resolve the
+ ;; branch below at compile time, it's more efficient to use the
+ ;; type-specific accessor.
+ (if (eq (cl-struct-sequence-type struct-type) 'vector)
+ (aref inst (cl-struct-slot-offset struct-type slot-name))
+ (nth (cl-struct-slot-offset struct-type slot-name) inst)))