Skip to content

Commit a4517b8

Browse files
committed
Add common trait snippets for Rust
1 parent 0462da9 commit a4517b8

File tree

8 files changed

+87
-0
lines changed

8 files changed

+87
-0
lines changed

snippets/rust-mode/asref

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: impl AsRef<Type> for Type
3+
# key: asref
4+
# uuid: asref
5+
# --
6+
impl std::convert::AsRef<${1:Type}> for ${2:Type} {
7+
fn as_ref(&self) -> &$2 {
8+
$0
9+
}
10+
}

snippets/rust-mode/deref

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: impl Deref for Type
3+
# key: deref
4+
# uuid: deref
5+
# --
6+
impl std::ops::Deref for ${1:Type} {
7+
type Target = ${2:Type};
8+
fn deref(&self) -> &Self::Target {
9+
&self.$0
10+
}
11+
}

snippets/rust-mode/deref_mut

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: impl DerefMut for Type
3+
# key: deref_mut
4+
# uuid: deref_mut
5+
# --
6+
impl std::ops::DerefMut for ${1:Type} {
7+
fn deref_mut(&mut self) -> &mut Self::Target {
8+
&mut self.$0
9+
}
10+
}

snippets/rust-mode/disperror

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- mode: snippet -*-
2+
# name: Display and Error Traits
3+
# key: disperror
4+
# uuid: disperror
5+
# --
6+
impl Display for $1 {
7+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8+
write!(f, "{}", $0)
9+
}
10+
}
11+
12+
impl std::error::Error for ${1:Type} {
13+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
14+
None
15+
}
16+
}

snippets/rust-mode/error

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: impl Error for Type { fn source(...) }
3+
# key: error
4+
# uuid: error
5+
# --
6+
impl std::error::Error for ${1:Type} {
7+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
8+
$0
9+
None
10+
}
11+
}

snippets/rust-mode/iterator

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -*- mode: snippet -*-
2+
# name: impl Iterator for Type
3+
# key: iterator
4+
# uuid: iterator
5+
# --
6+
impl Iterator for ${1:Type} {
7+
type Item = ${2:Type};
8+
fn next(&mut self) -> Option<Self::Item> {
9+
$0
10+
}
11+
}

snippets/rust-mode/macro

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- mode: snippet -*-
2+
# name: macro_rules! name { (..) => (..); }
3+
# key: macro
4+
# uuid: macro
5+
# --
6+
macro_rules! ${1:name} {
7+
($2) => ($3);
8+
}

snippets/rust-mode/partial

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- mode: snippet -*-
2+
# name: impl PartialEq for Type
3+
# key: partial
4+
# uuid: partial
5+
# --
6+
impl PartialEq for ${1:Type} {
7+
fn eq(&self, other: &Self) -> bool {
8+
$0
9+
}
10+
}

0 commit comments

Comments
 (0)