git.delta.rocks / jrsonnet / refs/commits / 1d941f87f4d5

difftreelog

feat Spanned::{map,as_ref}

xoxzrqynYaroslav Bolyukin2026-04-25parent: #d6e3ee6.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-ir/src/expr.rsdiffbeforeafterboth
1use std::{1use std::{
2 fmt::{self, Debug, Display},2 fmt::{self, Debug, Display},
3 ops::Deref,3 ops::{Deref, RangeInclusive},
4 rc::Rc,4 rc::Rc,
5};5};
66
465 pub fn belongs_to(&self, other: &Span) -> bool {465 pub fn belongs_to(&self, other: &Span) -> bool {
466 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2466 other.0 == self.0 && other.1 <= self.1 && other.2 >= self.2
467 }467 }
468 pub fn range(&self) -> RangeInclusive<usize> {
469 let start = self.1;
470 let mut end = self.2;
471 if end > start {
472 // Because it is originally exclusive
473 end -= 1;
474 }
475 start as usize..=end as usize
476 }
468}477}
469478
470#[cfg(target_pointer_width = "64")]479#[cfg(target_pointer_width = "64")]
492 pub fn new(value: T, span: Span) -> Self {501 pub fn new(value: T, span: Span) -> Self {
493 Self { value, span }502 Self { value, span }
494 }503 }
504 pub fn map<U: Acyclic>(self, v: impl FnOnce(T) -> U) -> Spanned<U> {
505 Spanned {
506 span: self.span,
507 value: v(self.value),
508 }
509 }
510 pub fn as_ref<'a>(&'a self) -> Spanned<&'a T>
511 where
512 &'a T: Acyclic,
513 {
514 Spanned {
515 span: self.span.clone(),
516 value: &self.value,
517 }
518 }
495}519}
496520
497impl<T: Debug + Acyclic> Debug for Spanned<T> {521impl<T: Debug + Acyclic> Debug for Spanned<T> {