A powerful Rust procedural macro crate that allows you to create aliases for types, keywords, and even complex syntax patterns. Make Rust syntax your own by creating custom aliases for anything!
- Type Aliases: Create custom names for existing types (
i64
βMyInt
) - Keyword Aliases: Alias Rust keywords like
struct
with attributes - Struct Aliases: Create aliases for your custom structs
- Zero Runtime Cost: All expansions happen at compile time
- Flexible Syntax: Support for both simple types and complex patterns
Add this to your Cargo.toml
:
[dependencies]
alias_macros = "0.1.1"
Or use cargo to add it:
cargo add alias_macros
use alias_macros::define;
// Create a type alias
define!(MyInt = i64);
fn main() {
let number: MyInt!() = 42;
println!("My number: {}", number);
}
Perfect for creating more descriptive type names or aliasing complex types:
use alias_macros::define;
define!(MyInt = i64);
define!(str = String);
define!(char = String);
fn main() {
let number: MyInt!() = 100;
let text: char!() = <char!()>::from("I'm a String not a char");
println!("Number: {}", number);
println!("Text: {}", text);
}
Create TypeScript-like interfaces or custom struct syntax:
use alias_macros::define;
// Create an "interface" keyword that's actually a struct with Debug derive
define!(interface = #[derive(Debug)]struct);
// Use your new interface keyword
interface!(User {
id: i64,
name: String,
});
fn main() {
let user = User {
id: 1,
name: "Alice".to_string(),
};
println!("User: {:?}", user);
}
Alias your custom structs for easier instantiation:
use alias_macros::define;
#[derive(Debug)]
pub struct DataContainer {
value: String,
}
// Create an alias for your struct
define!(Container = DataContainer);
fn main() {
// Use the alias to create instances
let data = Container!({
value: <String>::from("Hello World")
});
println!("Data: {:?}", data);
}
Here's a comprehensive example showing multiple usage patterns:
use alias_macros::define;
// Type aliases
define!(MyInt = i64);
define!(str = String);
define!(char = String);
// Struct keyword alias
define!(interface = #[derive(Debug)]struct);
// Create a struct using the interface alias
interface!(Nothing {
num: MyInt!()
});
// Regular struct
#[derive(Debug)]
pub struct something {
str: str!()
}
// Struct type alias
define!(strignStruct = something);
fn main() {
// Using type aliases
let v: MyInt!() = 100;
let char: char!() = <char!()>::from("I'm a String not a char");
// Using struct created with interface alias
let s = Nothing {
num: 1
};
// Using struct type alias
let a = strignStruct!({
str: <str!()>::from("Shubh")
});
println!("--------------------------------------------------------");
println!("I'm a struct, but a TS nerd initialized me calling Interface, but I still work: {:?}", s);
println!("");
println!("A dumb guy created me with a different name and then aliased me to call strignStruct, anyways I'm: {:?}", a);
println!("");
println!("I'm called MyInt, and I'm a i64, Check this out: {}", v);
println!("");
println!("People call me char, but secretly I'm a String: {}", char);
println!("--------------------------------------------------------");
}
Coming from TypeScript or other languages? Create familiar syntax:
define!(number = i64);
define!(string = String);
define!(boolean = bool);
define!(interface = #[derive(Debug, Clone)]struct);
Create specialized syntax for your domain:
// Game development
define!(Entity = u64);
define!(Component = struct);
define!(Health = i32);
// Web development
define!(UserId = u64);
define!(Email = String);
define!(Token = String);
Make your code more readable:
define!(Timestamp = i64);
define!(Price = f64);
define!(Count = usize);
define!(alias_name = replacement);
// Declaration
define!(MyType = SomeType);
// Usage
let var: MyType!() = value;
// With associated functions
let var: MyType!() = <MyType!()>::from(value);
// Declaration
define!(keyword = #[derive(Debug)]struct);
// Usage
keyword!(StructName {
field: Type,
});
// Declaration (after defining the struct)
define!(Alias = OriginalStruct);
// Usage
let instance = Alias!({
field: value
});
The define!
macro creates a macro_rules!
macro with your specified name:
- Compile Time:
define!(MyInt = i64)
generates a new macroMyInt!()
- Expansion:
MyInt!()
expands toi64
,MyInt!(args)
expands toi64 args
- Zero Cost: Everything is resolved at compile time with no runtime overhead
- Associated Functions: Use
<alias!()>::method()
syntax for calling associated functions - Struct Creation: For struct aliases, use
Alias!({ field: value })
syntax - Scope: Aliases are available in the scope where they're defined
- Compile Time: All aliases are resolved at compile time
git clone https://github.com/shubhiscoding/alias_macros
cd alias_macros
cargo build
cargo test
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Made with β€οΈ by Shubh Kesharwani