Skip to content

Commit

Permalink
Return &[T] rather than Vec<T> and similar in getters (#75)
Browse files Browse the repository at this point in the history
There are several return types in asg.rs that are improved.
This causes a bit of a change in the API. So validate.rs is updated
External users, that is the qiskit importer, will likely need to
be updated.
  • Loading branch information
jlapeyre committed Feb 7, 2024
1 parent 1a701a3 commit d1b390f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
40 changes: 22 additions & 18 deletions crates/oq3_semantics/src/asg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Program {
}
}

pub fn stmts(&self) -> &Vec<Stmt> {
pub fn stmts(&self) -> &[Stmt] {
&self.stmts
}

Expand All @@ -67,8 +67,8 @@ impl Program {
self.version = Some(version);
}

pub fn version(&self) -> &Option<OpenQASMVersion> {
&self.version
pub fn version(&self) -> Option<&OpenQASMVersion> {
self.version.as_ref()
}

// FIXME: must exist idiomatic rust for managing these modes
Expand Down Expand Up @@ -158,11 +158,11 @@ impl TExpr {
}

pub fn get_type(&self) -> &Type {
&(self.ty)
&self.ty
}

pub fn expression(&self) -> &Expr {
&(self.expression)
&self.expression
}
}

Expand Down Expand Up @@ -270,7 +270,7 @@ impl AnnotatedStmt {
&self.stmt
}

pub fn annotations(&self) -> &Vec<Annotation> {
pub fn annotations(&self) -> &[Annotation] {
&self.annotations
}
}
Expand Down Expand Up @@ -346,6 +346,10 @@ impl HardwareQubit {
}
}

pub fn identifier(&self) -> &str {
&self.identifier
}

pub fn to_texpr(self) -> TExpr {
TExpr::new(Expr::HardwareQubit(self), Type::HardwareQubit)
}
Expand Down Expand Up @@ -458,8 +462,8 @@ impl DeclareClassical {
&self.name
}

pub fn initializer(&self) -> &Option<Box<TExpr>> {
&self.initializer
pub fn initializer(&self) -> Option<&TExpr> {
self.initializer.as_deref()
}

pub fn to_stmt(self) -> Stmt {
Expand Down Expand Up @@ -536,11 +540,11 @@ impl GateDeclaration {
&self.name
}

pub fn params(&self) -> &Option<Vec<SymbolIdResult>> {
&self.params
pub fn params(&self) -> Option<&[SymbolIdResult]> {
self.params.as_deref()
}

pub fn qubits(&self) -> &Vec<SymbolIdResult> {
pub fn qubits(&self) -> &[SymbolIdResult] {
&self.qubits
}

Expand Down Expand Up @@ -586,8 +590,8 @@ impl Barrier {
Barrier { qubits }
}

pub fn qubits(&self) -> &Option<Vec<TExpr>> {
&self.qubits
pub fn qubits(&self) -> Option<&[TExpr]> {
self.qubits.as_deref()
}
}

Expand Down Expand Up @@ -648,12 +652,12 @@ impl GateCall {
&self.name
}

pub fn qubits(&self) -> &Vec<TExpr> {
pub fn qubits(&self) -> &[TExpr] {
&self.qubits
}

pub fn params(&self) -> &Option<Vec<TExpr>> {
&self.params
pub fn params(&self) -> Option<&[TExpr]> {
self.params.as_deref()
}

pub fn modifiers(&self) -> &[GateModifier] {
Expand Down Expand Up @@ -1110,8 +1114,8 @@ impl If {
&self.then_branch
}

pub fn else_branch(&self) -> &Option<Block> {
&self.else_branch
pub fn else_branch(&self) -> Option<&Block> {
self.else_branch.as_ref()
}

pub fn to_stmt(self) -> Stmt {
Expand Down
6 changes: 3 additions & 3 deletions crates/oq3_semantics/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<T: SymTrait> WalkSymbols<T> for Program {
}
}

impl<V, T: SymTrait> WalkSymbols<T> for &Vec<V>
impl<V, T: SymTrait> WalkSymbols<T> for &[V]
where
V: WalkSymbols<T>,
{
Expand All @@ -59,7 +59,7 @@ where
}
}

impl<V, T: SymTrait> WalkSymbols<T> for Box<V>
impl<V, T: SymTrait> WalkSymbols<T> for &Box<V>
where
V: WalkSymbols<T>,
{
Expand All @@ -68,7 +68,7 @@ where
}
}

impl<T: SymTrait> WalkSymbols<T> for TExpr {
impl<T: SymTrait> WalkSymbols<T> for &TExpr {
fn walk_symbols(&self, context: &mut SymContext<T>) {
self.expression().walk_symbols(context);
}
Expand Down

0 comments on commit d1b390f

Please sign in to comment.