Consolidate Operand code

This commit is contained in:
David Tolnay 2019-05-08 16:58:01 -07:00
parent 097b9cec00
commit e627fa8983
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 41 additions and 58 deletions

View File

@ -17,6 +17,7 @@ mod fold;
mod full;
mod gen;
mod json;
mod operand;
mod parse;
mod version;
mod visit;

38
codegen/src/operand.rs Normal file
View File

@ -0,0 +1,38 @@
use proc_macro2::TokenStream;
use quote::quote;
pub enum Operand {
Borrowed(TokenStream),
Owned(TokenStream),
}
pub use self::Operand::*;
impl Operand {
pub fn tokens(&self) -> &TokenStream {
match self {
Borrowed(n) | Owned(n) => n,
}
}
pub fn ref_tokens(&self) -> TokenStream {
match self {
Borrowed(n) => n.clone(),
Owned(n) => quote!(&#n),
}
}
pub fn ref_mut_tokens(&self) -> TokenStream {
match self {
Borrowed(n) => n.clone(),
Owned(n) => quote!(&mut #n),
}
}
pub fn owned_tokens(&self) -> TokenStream {
match self {
Borrowed(n) => quote!(*#n),
Owned(n) => n.clone(),
}
}
}

View File

@ -1,3 +1,4 @@
use crate::operand::*;
use crate::{file, full, gen};
use inflections::Inflect;
use proc_macro2::{Span, TokenStream};
@ -17,35 +18,6 @@ fn under_name(name: &str) -> Ident {
Ident::new(&name.to_snake_case(), Span::call_site())
}
enum Operand {
Borrowed(TokenStream),
Owned(TokenStream),
}
use self::Operand::*;
impl Operand {
fn tokens(&self) -> &TokenStream {
match self {
Borrowed(n) | Owned(n) => n,
}
}
fn ref_tokens(&self) -> TokenStream {
match self {
Borrowed(n) => n.clone(),
Owned(n) => quote!(&#n),
}
}
fn owned_tokens(&self) -> TokenStream {
match self {
Borrowed(n) => quote!(*#n),
Owned(n) => n.clone(),
}
}
}
fn simple_visit(item: &str, name: &Operand) -> TokenStream {
let ident = under_name(item);
let method = Ident::new(&format!("visit_{}", ident), Span::call_site());

View File

@ -1,3 +1,4 @@
use crate::operand::*;
use crate::{file, full, gen};
use inflections::Inflect;
use proc_macro2::{Span, TokenStream};
@ -17,35 +18,6 @@ fn under_name(name: &str) -> Ident {
Ident::new(&name.to_snake_case(), Span::call_site())
}
enum Operand {
Borrowed(TokenStream),
Owned(TokenStream),
}
use self::Operand::*;
impl Operand {
fn tokens(&self) -> &TokenStream {
match self {
Borrowed(n) | Owned(n) => n,
}
}
fn ref_mut_tokens(&self) -> TokenStream {
match self {
Borrowed(n) => n.clone(),
Owned(n) => quote!(&mut #n),
}
}
fn owned_tokens(&self) -> TokenStream {
match self {
Borrowed(n) => quote!(*#n),
Owned(n) => n.clone(),
}
}
}
fn simple_visit(item: &str, name: &Operand) -> TokenStream {
let ident = under_name(item);
let method = Ident::new(&format!("visit_{}_mut", ident), Span::call_site());