| 1 |
/* Copyright 2018 Mozilla Foundation |
| 2 |
* |
| 3 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
* you may not use this file except in compliance with the License. |
| 5 |
* You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software |
| 10 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
* See the License for the specific language governing permissions and |
| 13 |
* limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
use crate::{BinaryReader, FromReader, OperatorsReader, Result}; |
| 17 |
use core::fmt; |
| 18 |
|
| 19 |
/// Represents an initialization expression. |
| 20 |
#[derive(Clone)] |
| 21 |
pub struct ConstExpr<'a> { |
| 22 |
reader: BinaryReader<'a>, |
| 23 |
} |
| 24 |
|
| 25 |
impl PartialEq for ConstExpr<'_> { |
| 26 |
fn eq(&self, other: &Self) -> bool { |
| 27 |
self.reader.remaining_buffer() == other.reader.remaining_buffer() |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl Eq for ConstExpr<'_> {} |
| 32 |
|
| 33 |
impl<'a> ConstExpr<'a> { |
| 34 |
/// Constructs a new `ConstExpr` from the given data and offset. |
| 35 |
pub fn new(reader: BinaryReader<'a>) -> ConstExpr<'a> { |
| 36 |
ConstExpr { reader } |
| 37 |
} |
| 38 |
|
| 39 |
/// Gets a binary reader for the initialization expression. |
| 40 |
pub fn get_binary_reader(&self) -> BinaryReader<'a> { |
| 41 |
self.reader.clone() |
| 42 |
} |
| 43 |
|
| 44 |
/// Gets an operators reader for the initialization expression. |
| 45 |
pub fn get_operators_reader(&self) -> OperatorsReader<'a> { |
| 46 |
OperatorsReader::new(self.get_binary_reader()) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl<'a> FromReader<'a> for ConstExpr<'a> { |
| 51 |
fn from_reader(reader: &mut BinaryReader<'a>) -> Result<Self> { |
| 52 |
// FIXME(#188) ideally shouldn't need to skip here |
| 53 |
let reader = reader.skip(|r| r.skip_const_expr())?; |
| 54 |
Ok(ConstExpr { reader }) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl fmt::Debug for ConstExpr<'_> { |
| 59 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 60 |
f.debug_struct("ConstExpr") |
| 61 |
.field("offset", &self.reader.original_position()) |
| 62 |
.field("data", &self.reader.remaining_buffer()) |
| 63 |
.finish() |
| 64 |
} |
| 65 |
} |