Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions src/values/basic_value_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ impl<'ctx> Operand<'ctx> {
matches!(self, Self::Block(_))
}

/// Determines if the [Operand] is a [Metadata].
#[inline]
#[must_use]
pub fn is_metadata(self) -> bool {
matches!(self, Self::Metadata(_))
}

/// If the [Operand] is a [BasicValueEnum], map it into [Option::Some].
#[inline]
#[must_use]
Expand All @@ -52,6 +59,16 @@ impl<'ctx> Operand<'ctx> {
}
}

/// If the [Operand] is a [Metadata], map it into [Option::Some].
#[inline]
#[must_use]
pub fn metadata(self) -> Option<MetadataValue<'ctx>> {
match self {
Self::Metadata(md) => Some(md),
_ => None,
}
}

/// Expect [BasicValueEnum], panic with the message if it is not.
#[inline]
#[must_use]
Expand All @@ -74,20 +91,51 @@ impl<'ctx> Operand<'ctx> {
}
}

/// Expect [Metadata], panic with the message if it is not.
#[inline]
#[must_use]
#[track_caller]
pub fn expect_metadata(self, msg: &str) -> MetadataValue<'ctx> {
match self {
Self::Metadata(md) => md,
_ => panic!("{msg}"),
}
}

/// Unwrap [BasicValueEnum]. Will panic if it is not.
#[inline]
#[must_use]
#[track_caller]
pub fn unwrap_value(self) -> BasicValueEnum<'ctx> {
self.expect_value("Called unwrap_value() on UsedValue::Block.")
match self {
Self::Value(value) => value,
Self::Block(_) => panic!("Called unwrap_value() on UsedValue::Block."),
Self::Metadata(_) => panic!("Called unwrap_value() on UsedValue::Metadata."),
}
}

/// Unwrap [BasicBlock]. Will panic if it is not.
#[inline]
#[must_use]
#[track_caller]
pub fn unwrap_block(self) -> BasicBlock<'ctx> {
self.expect_block("Called unwrap_block() on UsedValue::Value.")
match self {
Self::Value(_) => panic!("Called unwrap_value() on UsedValue::Value."),
Self::Block(block) => block,
Self::Metadata(_) => panic!("Called unwrap_value() on UsedValue::Metadata."),
}
}

/// Unwrap [Metadata]. Will panic if it is not.
#[inline]
#[must_use]
#[track_caller]
pub fn unwrap_metadata(self) -> MetadataValue<'ctx> {
match self {
Self::Value(_) => panic!("Called unwrap_value() on UsedValue::Value."),
Self::Block(_) => panic!("Called unwrap_value() on UsedValue::Block."),
Self::Metadata(md) => md,
}
Comment thread
Yoric marked this conversation as resolved.
}
}

Expand Down
Loading