Skip to content

Commit 199bc65

Browse files
committed
Made Encoding::from_str an unsafe method.
1 parent c0002ba commit 199bc65

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

core/encode.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub struct Encoding {
1414
}
1515

1616
impl Encoding {
17-
pub fn from_str(code: &'static str) -> Encoding {
17+
/// Constructs an `Encoding` from its string representation.
18+
/// Unsafe because the caller must ensure the string is a valid encoding.
19+
pub unsafe fn from_str(code: &'static str) -> Encoding {
1820
Encoding { code: code }
1921
}
2022

@@ -61,7 +63,7 @@ pub unsafe trait Encode : PhantomFn<Self> {
6163
macro_rules! encode_impls {
6264
($($t:ty : $s:expr,)*) => ($(
6365
unsafe impl Encode for $t {
64-
fn encode() -> Encoding { Encoding::from_str($s) }
66+
fn encode() -> Encoding { unsafe { Encoding::from_str($s) } }
6567
}
6668
)*);
6769
}
@@ -108,27 +110,27 @@ macro_rules! encode_message_impl {
108110
);
109111
($code:expr, $name:ident, $($t:ident),*) => (
110112
unsafe impl<'a $(, $t)*> $crate::Encode for &'a $name<$($t),*> {
111-
fn encode() -> Encoding { Encoding::from_str($code) }
113+
fn encode() -> Encoding { unsafe { Encoding::from_str($code) } }
112114
}
113115

114116
unsafe impl<'a $(, $t)*> $crate::Encode for &'a mut $name<$($t),*> {
115-
fn encode() -> Encoding { Encoding::from_str($code) }
117+
fn encode() -> Encoding { unsafe { Encoding::from_str($code) } }
116118
}
117119

118120
unsafe impl<'a $(, $t)*> $crate::Encode for Option<&'a $name<$($t),*>> {
119-
fn encode() -> Encoding { Encoding::from_str($code) }
121+
fn encode() -> Encoding { unsafe { Encoding::from_str($code) } }
120122
}
121123

122124
unsafe impl<'a $(, $t)*> $crate::Encode for Option<&'a mut $name<$($t),*>> {
123-
fn encode() -> Encoding { Encoding::from_str($code) }
125+
fn encode() -> Encoding { unsafe { Encoding::from_str($code) } }
124126
}
125127

126128
unsafe impl<$($t),*> $crate::Encode for *const $name<$($t),*> {
127-
fn encode() -> Encoding { Encoding::from_str($code) }
129+
fn encode() -> Encoding { unsafe { Encoding::from_str($code) } }
128130
}
129131

130132
unsafe impl<$($t),*> $crate::Encode for *mut $name<$($t),*> {
131-
fn encode() -> Encoding { Encoding::from_str($code) }
133+
fn encode() -> Encoding { unsafe { Encoding::from_str($code) } }
132134
}
133135
);
134136
}

core/test_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ pub struct CustomStruct {
5353

5454
unsafe impl Encode for CustomStruct {
5555
fn encode() -> Encoding {
56-
Encoding::from_str(encode!(struct CustomStruct { u64, u64, u64, u64 }))
56+
let code = encode!(struct CustomStruct { u64, u64, u64, u64 });
57+
unsafe { Encoding::from_str(code) }
5758
}
5859
}
5960

0 commit comments

Comments
 (0)