Skip to content

Commit af45abc

Browse files
authored
Merge pull request rust-lang#103 from wasmerio/try_from
Add TryFrom traits from BasicValueEnum and BasicTypeEnum.
2 parents a34b58c + d463ec6 commit af45abc

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

.travis.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ matrix:
4545
packages:
4646
- *BASE_PACKAGES
4747
- llvm-3.6-dev
48-
rust: nightly-2019-01-15
48+
rust: nightly-2019-07-25
4949
- env:
5050
- LLVM_VERSION="3.7"
5151
<<: *BASE
@@ -57,7 +57,7 @@ matrix:
5757
packages:
5858
- *BASE_PACKAGES
5959
- llvm-3.7-dev
60-
rust: nightly-2019-01-15
60+
rust: nightly-2019-07-25
6161
- env:
6262
- LLVM_VERSION="3.8"
6363
<<: *BASE
@@ -69,7 +69,7 @@ matrix:
6969
packages:
7070
- *BASE_PACKAGES
7171
- llvm-3.8-dev
72-
rust: nightly-2019-01-15
72+
rust: nightly-2019-07-25
7373
# 3.9 seems to have a linking issue :/
7474
# - env:
7575
# - LLVM_VERSION="3.9"
@@ -93,7 +93,7 @@ matrix:
9393
packages:
9494
- *BASE_PACKAGES
9595
- llvm-4.0-dev
96-
rust: nightly-2019-01-15
96+
rust: nightly-2019-07-25
9797
- env:
9898
- LLVM_VERSION="5.0"
9999
<<: *BASE
@@ -105,7 +105,7 @@ matrix:
105105
packages:
106106
- *BASE_PACKAGES
107107
- llvm-5.0-dev
108-
rust: nightly-2019-01-15
108+
rust: nightly-2019-07-25
109109
- env:
110110
- LLVM_VERSION="6.0"
111111
<<: *BASE
@@ -117,7 +117,7 @@ matrix:
117117
packages:
118118
- *BASE_PACKAGES
119119
- llvm-6.0-dev
120-
rust: nightly-2019-01-15
120+
rust: nightly-2019-07-25
121121
- env:
122122
- LLVM_VERSION="7.0"
123123
<<: *BASE
@@ -129,7 +129,7 @@ matrix:
129129
packages:
130130
- *BASE_PACKAGES
131131
- llvm-7-dev
132-
rust: nightly-2019-01-15
132+
rust: nightly-2019-07-25
133133
- env:
134134
- LLVM_VERSION="8.0"
135135
<<: *BASE
@@ -141,7 +141,7 @@ matrix:
141141
packages:
142142
- *BASE_PACKAGES
143143
- llvm-8-dev
144-
rust: nightly-2019-01-15
144+
rust: nightly-2019-07-25
145145
- deploy: # Documentation build; Only latest supported LLVM version for now
146146
provider: pages
147147
skip-cleanup: true
@@ -188,7 +188,7 @@ env:
188188
- RUSTFLAGS="-C link-dead-code -C target-cpu=native -l ffi"
189189

190190
after_success: |
191-
if [[ "$TRAVIS_RUST_VERSION" == nightly-2019-01-15 ]]; then
191+
if [[ "$TRAVIS_RUST_VERSION" == nightly-2019-07-25 ]]; then
192192
cargo tarpaulin --features "llvm${LLVM_VERSION_DASH}" --ignore-tests --out Xml
193193
bash <(curl -s https://codecov.io/bash)
194194
fi

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Inkwell aims to help you pen your own programming languages by safely wrapping l
1313

1414
## Requirements
1515

16-
* Rust 1.31+
16+
* Rust 1.34+
1717
* Rust Stable, Beta, or Nightly
1818
* LLVM 3.6, 3.7, 3.8, 3.9, 4.0, 5.0, 6.0, 7.0, or 8.0
1919

src/types/traits.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use llvm_sys::prelude::LLVMTypeRef;
22

3+
use std::convert::TryFrom;
34
use std::fmt::Debug;
45

56
use crate::AddressSpace;
@@ -149,3 +150,25 @@ impl PointerMathType for VectorType {
149150
type ValueType = VectorValue;
150151
type PtrConvType = VectorType;
151152
}
153+
154+
macro_rules! impl_try_from_basic_type_enum {
155+
($type_name:ident) => (
156+
impl TryFrom<BasicTypeEnum> for $type_name {
157+
type Error = &'static str;
158+
159+
fn try_from(ty: BasicTypeEnum) -> Result<Self, Self::Error> {
160+
match ty {
161+
BasicTypeEnum::$type_name(ty) => Ok(ty),
162+
_ => Err("bad try from"),
163+
}
164+
}
165+
}
166+
)
167+
}
168+
169+
impl_try_from_basic_type_enum!(ArrayType);
170+
impl_try_from_basic_type_enum!(FloatType);
171+
impl_try_from_basic_type_enum!(IntType);
172+
impl_try_from_basic_type_enum!(PointerType);
173+
impl_try_from_basic_type_enum!(StructType);
174+
impl_try_from_basic_type_enum!(VectorType);

src/values/traits.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use llvm_sys::prelude::LLVMValueRef;
22
use llvm_sys::core::{LLVMConstExtractValue, LLVMConstInsertValue};
33

4+
use std::convert::TryFrom;
45
use std::fmt::Debug;
56

67
use crate::values::{ArrayValue, AggregateValueEnum, BasicValueUse, CallSiteValue, GlobalValue, StructValue, BasicValueEnum, AnyValueEnum, IntValue, FloatValue, PointerValue, PhiValue, VectorValue, FunctionValue, InstructionValue, Value};
7-
use crate::types::{IntMathType, FloatMathType, PointerMathType, IntType, FloatType, PointerType, VectorType};
8+
use crate::types::{ArrayType, BasicTypeEnum, IntMathType, FloatMathType, PointerMathType, IntType, FloatType, PointerType, StructType, VectorType};
89

910
// This is an ugly privacy hack so that Type can stay private to this module
1011
// and so that super traits using this trait will be not be implementable
@@ -124,3 +125,25 @@ trait_value_set! {BasicValue: ArrayValue, BasicValueEnum, AggregateValueEnum, In
124125
math_trait_value_set! {IntMathValue: (IntValue => IntType), (VectorValue => VectorType)}
125126
math_trait_value_set! {FloatMathValue: (FloatValue => FloatType), (VectorValue => VectorType)}
126127
math_trait_value_set! {PointerMathValue: (PointerValue => PointerType), (VectorValue => VectorType)}
128+
129+
macro_rules! impl_try_from_basic_value_enum {
130+
($value_name:ident) => (
131+
impl TryFrom<BasicValueEnum> for $value_name {
132+
type Error = &'static str;
133+
134+
fn try_from(value: BasicValueEnum) -> Result<Self, Self::Error> {
135+
match value {
136+
BasicValueEnum::$value_name(value) => Ok(value),
137+
_ => Err("bad try from"),
138+
}
139+
}
140+
}
141+
)
142+
}
143+
144+
impl_try_from_basic_value_enum!(ArrayValue);
145+
impl_try_from_basic_value_enum!(IntValue);
146+
impl_try_from_basic_value_enum!(FloatValue);
147+
impl_try_from_basic_value_enum!(PointerValue);
148+
impl_try_from_basic_value_enum!(StructValue);
149+
impl_try_from_basic_value_enum!(VectorValue);

0 commit comments

Comments
 (0)