Skip to content

Commit 5dc95b6

Browse files
committed
use type ascription
1 parent b1f06c4 commit 5dc95b6

File tree

10 files changed

+41
-68
lines changed

10 files changed

+41
-68
lines changed

src/rmo/impls/c_string.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ impl<P> ToRmo<P, CStr, CString<P>> for [i8]
4444
where P: MemPool,
4545
{
4646
fn to_rmo_with<'a>(&'a self, pool: P) -> Result<Rmo<'a, CStr, CString<P>>> {
47-
let bytes: &[u8] = self.as_ref();
48-
bytes.to_rmo_with(pool)
47+
(self.as_ref():&[u8]).to_rmo_with(pool)
4948
}
5049
}
5150

5251
impl<P> ToRmo<P, CStr, CString<P>> for str
5352
where P: MemPool,
5453
{
5554
fn to_rmo_with<'a>(&'a self, pool: P) -> Result<Rmo<'a, CStr, CString<P>>> {
56-
let bytes: &[u8] = self.as_ref();
57-
bytes.to_rmo_with(pool)
55+
(self.as_ref():&[u8]).to_rmo_with(pool)
5856
}
5957
}
6058

@@ -74,8 +72,7 @@ impl<P> ToRmo<P, CStr, CString<P>> for ByteStr
7472
where P: MemPool,
7573
{
7674
fn to_rmo_with<'a>(&'a self, pool: P) -> Result<Rmo<'a, CStr, CString<P>>> {
77-
let bytes: &[u8] = self.as_ref();
78-
bytes.to_rmo_with(pool)
75+
(self.as_ref():&[u8]).to_rmo_with(pool)
7976
}
8077
}
8178

src/rmo/impls/no_null_string.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ impl<H> ToOwned<H> for NoNullStr
1313
{
1414
type Owned = NoNullString<H>;
1515
fn to_owned_with_pool(&self, pool: H) -> Result<NoNullString<H>> {
16-
let bytes: &[u8] = self.as_ref();
17-
bytes.to_owned_with_pool(pool).map(|o| unsafe {
16+
(self.as_ref():&[u8]).to_owned_with_pool(pool).map(|o| unsafe {
1817
NoNullString::from_bytes_unchecked(o)
1918
})
2019
}

src/rmo/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#![crate_name = "lrs_rmo"]
66
#![crate_type = "lib"]
7+
#![feature(type_ascription)]
78
#![no_std]
89

910
extern crate lrs_base as base;

src/str_one/conv.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ impl TryAsRef<CStr> for [u8] {
6565

6666
impl TryAsRef<CStr> for [i8] {
6767
fn try_as_ref(&self) -> Result<&CStr> {
68-
let bytes: &[u8] = self.as_ref();
69-
bytes.try_as_ref()
68+
(self.as_ref():&[u8]).try_as_ref()
7069
}
7170
}
7271

@@ -89,8 +88,7 @@ impl TryAsMut<NoNullStr> for [u8] {
8988

9089
impl TryAsMut<CStr> for [i8] {
9190
fn try_as_mut(&mut self) -> Result<&mut CStr> {
92-
let bytes: &mut [u8] = self.as_mut();
93-
bytes.try_as_mut()
91+
(self.as_mut():&mut [u8]).try_as_mut()
9492
}
9593
}
9694

@@ -107,23 +105,20 @@ impl TryAsMut<CStr> for [u8] {
107105

108106
impl AsRef<ByteStr> for str {
109107
fn as_ref(&self) -> &ByteStr {
110-
let bytes: &[u8] = self.as_ref();
111-
bytes.as_ref()
108+
(self.as_ref():&[u8]).as_ref()
112109
}
113110
}
114111
impl_try_as_ref!(ByteStr, str);
115112

116113
impl TryAsRef<NoNullStr> for str {
117114
fn try_as_ref(&self) -> Result<&NoNullStr> {
118-
let bytes: &[u8] = self.as_ref();
119-
bytes.try_as_ref()
115+
(self.as_ref():&[u8]).try_as_ref()
120116
}
121117
}
122118

123119
impl TryAsRef<CStr> for str {
124120
fn try_as_ref(&self) -> Result<&CStr> {
125-
let bytes: &[u8] = self.as_ref();
126-
bytes.try_as_ref()
121+
(self.as_ref():&[u8]).try_as_ref()
127122
}
128123
}
129124

@@ -136,22 +131,19 @@ impl_try_as_ref!([u8], ByteStr);
136131

137132
impl TryAsRef<str> for ByteStr {
138133
fn try_as_ref(&self) -> Result<&str> {
139-
let bytes: &[u8] = self.as_ref();
140-
bytes.try_as_ref()
134+
(self.as_ref():&[u8]).try_as_ref()
141135
}
142136
}
143137

144138
impl TryAsRef<NoNullStr> for ByteStr {
145139
fn try_as_ref(&self) -> Result<&NoNullStr> {
146-
let bytes: &[u8] = self.as_ref();
147-
bytes.try_as_ref()
140+
(self.as_ref():&[u8]).try_as_ref()
148141
}
149142
}
150143

151144
impl TryAsRef<CStr> for ByteStr {
152145
fn try_as_ref(&self) -> Result<&CStr> {
153-
let bytes: &[u8] = self.as_ref();
154-
bytes.try_as_ref()
146+
(self.as_ref():&[u8]).try_as_ref()
155147
}
156148
}
157149

@@ -164,15 +156,13 @@ impl_try_as_mut!([u8], ByteStr);
164156

165157
impl TryAsMut<NoNullStr> for ByteStr {
166158
fn try_as_mut(&mut self) -> Result<&mut NoNullStr> {
167-
let bytes: &mut [u8] = self.as_mut();
168-
bytes.try_as_mut()
159+
(self.as_mut():&mut [u8]).try_as_mut()
169160
}
170161
}
171162

172163
impl TryAsMut<CStr> for ByteStr {
173164
fn try_as_mut(&mut self) -> Result<&mut CStr> {
174-
let bytes: &mut [u8] = self.as_mut();
175-
bytes.try_as_mut()
165+
(self.as_mut():&mut [u8]).try_as_mut()
176166
}
177167
}
178168

@@ -185,16 +175,14 @@ impl_try_as_ref!([u8], NoNullStr);
185175

186176
impl AsRef<ByteStr> for NoNullStr {
187177
fn as_ref(&self) -> &ByteStr {
188-
let bytes: &[u8] = self.as_ref();
189-
bytes.as_ref()
178+
(self.as_ref():&[u8]).as_ref()
190179
}
191180
}
192181
impl_try_as_ref!(ByteStr, NoNullStr);
193182

194183
impl TryAsRef<str> for NoNullStr {
195184
fn try_as_ref(&self) -> Result<&str> {
196-
let bytes: &[u8] = self.as_ref();
197-
bytes.try_as_ref()
185+
(self.as_ref():&[u8]).try_as_ref()
198186
}
199187
}
200188

@@ -207,8 +195,7 @@ impl_try_as_ref!([u8], CStr);
207195

208196
impl AsRef<ByteStr> for CStr {
209197
fn as_ref(&self) -> &ByteStr {
210-
let bytes: &[u8] = self.as_ref();
211-
bytes.as_ref()
198+
(self.as_ref():&[u8]).as_ref()
212199
}
213200
}
214201
impl_try_as_ref!(ByteStr, CStr);
@@ -222,8 +209,7 @@ impl_try_as_ref!(NoNullStr, CStr);
222209

223210
impl TryAsRef<str> for CStr {
224211
fn try_as_ref(&self) -> Result<&str> {
225-
let bytes: &[u8] = self.as_ref();
226-
bytes.try_as_ref()
212+
(self.as_ref():&[u8]).try_as_ref()
227213
}
228214
}
229215

src/str_one/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#![crate_name = "lrs_str_one"]
66
#![crate_type = "lib"]
7+
#![feature(type_ascription)]
78
#![allow(mutable_transmutes)]
89
#![no_std]
910

src/str_two/conv.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,15 @@ impl<H> TryAsRef<NoNullStr> for String<H>
8686
where H: MemPool,
8787
{
8888
fn try_as_ref(&self) -> Result<&NoNullStr> {
89-
let bytes: &[u8] = self.as_ref();
90-
bytes.try_as_ref()
89+
(self.as_ref():&[u8]).try_as_ref()
9190
}
9291
}
9392

9493
impl<H> TryAsRef<CStr> for String<H>
9594
where H: MemPool,
9695
{
9796
fn try_as_ref(&self) -> Result<&CStr> {
98-
let bytes: &[u8] = self.as_ref();
99-
bytes.try_as_ref()
97+
(self.as_ref():&[u8]).try_as_ref()
10098
}
10199
}
102100

@@ -209,8 +207,7 @@ impl<T: ?Sized, H> TryFrom<T> for String<H>
209207
T: TryAsRef<str>,
210208
{
211209
fn try_from(t: &T) -> Result<String<H>> {
212-
let bytes: &[u8] = try!(t.try_as_ref()).as_ref();
213-
let vec = try!(bytes.try_to());
210+
let vec = try!((try!(t.try_as_ref()).as_ref():&[u8]).try_to());
214211
unsafe { Ok(String::from_bytes_unchecked(vec)) }
215212
}
216213
}

src/str_two/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#![crate_name = "lrs_str_two"]
66
#![crate_type = "lib"]
7+
#![feature(type_ascription)]
78
#![no_std]
89

910
extern crate lrs_arch_fns as arch_fns;

src/sys/lib.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#![crate_name = "lrs_sys"]
66
#![crate_type = "lib"]
7-
#![feature(custom_derive)]
7+
#![feature(type_ascription, custom_derive)]
88
#![no_std]
99

1010
extern crate lrs_base as base;
@@ -70,49 +70,43 @@ impl StrInfo {
7070
/// Retrieves information from the system and stores it in the object.
7171
pub fn update(&mut self) -> Result {
7272
try!(rv!(uname(&mut self.buf)));
73-
self.sysname_len = try!(TryAsRef::<CStr>::try_as_ref(&self.buf.sysname[..])).len() as u8;
74-
self.nodename_len = try!(TryAsRef::<CStr>::try_as_ref(&self.buf.nodename[..])).len() as u8;
75-
self.release_len = try!(TryAsRef::<CStr>::try_as_ref(&self.buf.release[..])).len() as u8;
76-
self.version_len = try!(TryAsRef::<CStr>::try_as_ref(&self.buf.version[..])).len() as u8;
77-
self.machine_len = try!(TryAsRef::<CStr>::try_as_ref(&self.buf.machine[..])).len() as u8;
78-
self.domainname_len = try!(TryAsRef::<CStr>::try_as_ref(&self.buf.domainname[..])).len() as u8;
73+
self.sysname_len = (try!(self.buf.sysname[..].try_as_ref()):&CStr).len() as u8;
74+
self.nodename_len = (try!(self.buf.nodename[..].try_as_ref()):&CStr).len() as u8;
75+
self.release_len = (try!(self.buf.release[..].try_as_ref()):&CStr).len() as u8;
76+
self.version_len = (try!(self.buf.version[..].try_as_ref()):&CStr).len() as u8;
77+
self.machine_len = (try!(self.buf.machine[..].try_as_ref()):&CStr).len() as u8;
78+
self.domainname_len = (try!(self.buf.domainname[..].try_as_ref()):&CStr).len() as u8;
7979
Ok(())
8080
}
8181

8282
/// Returns the name of the system.
8383
pub fn system_name(&self) -> &ByteStr {
84-
let bytes: &[u8] = self.buf.sysname[..self.sysname_len as usize].as_ref();
85-
bytes.as_ref()
84+
(self.buf.sysname[..self.sysname_len as usize].as_ref():&[u8]).as_ref()
8685
}
8786

8887
/// Returns the hostname of the system.
8988
pub fn host_name(&self) -> &ByteStr {
90-
let bytes: &[u8] = self.buf.nodename[..self.nodename_len as usize].as_ref();
91-
bytes.as_ref()
89+
(self.buf.nodename[..self.nodename_len as usize].as_ref():&[u8]).as_ref()
9290
}
9391

9492
/// Returns the kernel release of the system.
9593
pub fn release(&self) -> &ByteStr {
96-
let bytes: &[u8] = self.buf.release[..self.release_len as usize].as_ref();
97-
bytes.as_ref()
94+
(self.buf.release[..self.release_len as usize].as_ref():&[u8]).as_ref()
9895
}
9996

10097
/// Returns the kernel version of the system.
10198
pub fn version(&self) -> &ByteStr {
102-
let bytes: &[u8] = self.buf.version[..self.version_len as usize].as_ref();
103-
bytes.as_ref()
99+
(self.buf.version[..self.version_len as usize].as_ref():&[u8]).as_ref()
104100
}
105101

106102
/// Returns the machine.
107103
pub fn machine(&self) -> &ByteStr {
108-
let bytes: &[u8] = self.buf.machine[..self.machine_len as usize].as_ref();
109-
bytes.as_ref()
104+
(self.buf.machine[..self.machine_len as usize].as_ref():&[u8]).as_ref()
110105
}
111106

112107
/// Returns the domain name of the system.
113108
pub fn domain_name(&self) -> &ByteStr {
114-
let bytes: &[u8] = self.buf.domainname[..self.domainname_len as usize].as_ref();
115-
bytes.as_ref()
109+
(self.buf.domainname[..self.domainname_len as usize].as_ref():&[u8]).as_ref()
116110
}
117111
}
118112

src/vec/conv.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,26 +190,23 @@ impl<H> TryFrom<ByteStr> for Vec<u8, H>
190190
where H: MemPool + OutOf,
191191
{
192192
fn try_from(ts: &ByteStr) -> Result<Vec<u8, H>> {
193-
let bytes: &[u8] = ts.as_ref();
194-
bytes.try_to()
193+
(ts.as_ref():&[u8]).try_to()
195194
}
196195
}
197196

198197
impl<H> TryFrom<CStr> for Vec<u8, H>
199198
where H: MemPool + OutOf,
200199
{
201200
fn try_from(ts: &CStr) -> Result<Vec<u8, H>> {
202-
let bytes: &[u8] = ts.as_ref();
203-
bytes.try_to()
201+
(ts.as_ref():&[u8]).try_to()
204202
}
205203
}
206204

207205
impl<H> TryFrom<NoNullStr> for Vec<u8, H>
208206
where H: MemPool + OutOf,
209207
{
210208
fn try_from(ts: &NoNullStr) -> Result<Vec<u8, H>> {
211-
let bytes: &[u8] = ts.as_ref();
212-
bytes.try_to()
209+
(ts.as_ref():&[u8]).try_to()
213210
}
214211
}
215212

src/vec/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#![crate_name = "lrs_vec"]
66
#![crate_type = "lib"]
7-
#![feature(optin_builtin_traits)]
7+
#![feature(type_ascription, optin_builtin_traits)]
88
#![no_std]
99

1010
extern crate lrs_base as base;

0 commit comments

Comments
 (0)