Skip to content

Commit 52372da

Browse files
bluejekyllyjh0502
authored andcommitted
cleanup subspace
1 parent 571eb1c commit 52372da

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

foundationdb/src/subspace.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ impl Subspace {
6868
/// removed. `unpack` will return an error if the key is not in this Subspace or does not
6969
/// encode a well-formed Tuple.
7070
pub fn unpack<T: Tuple>(&self, key: &[u8]) -> Result<T, TupleError> {
71-
if !self.contains(key) {
71+
if !self.covers(key) {
7272
return Err(TupleError::InvalidData);
7373
}
7474
let key = &key[self.prefix.len()..];
7575
Tuple::decode(&key)
7676
}
7777

78-
/// `contains` returns true if the provided key starts with the prefix of this Subspace,
78+
/// `covers` returns true if the provided key starts with the prefix of this Subspace,
7979
/// indicating that the Subspace logically contains the key.
80-
pub fn contains(&self, key: &[u8]) -> bool {
80+
pub fn covers(&self, key: &[u8]) -> bool {
8181
key.starts_with(&self.prefix)
8282
}
8383

@@ -124,13 +124,13 @@ mod tests {
124124
}
125125

126126
#[test]
127-
fn contains() {
127+
fn covers() {
128128
let ss0 = Subspace::new(&(1,));
129129
let ss1 = Subspace::new(&(2,));
130130
let tup = (2, 3);
131131

132-
assert!(ss0.contains(&ss0.pack(&tup)));
133-
assert!(!ss1.contains(&ss0.pack(&tup)));
132+
assert!(ss0.covers(&ss0.pack(&tup)));
133+
assert!(!ss1.covers(&ss0.pack(&tup)));
134134
}
135135

136136
#[test]

foundationdb/src/tuple.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
use std;
1212
use std::io::Write;
13+
use std::string::FromUtf8Error;
1314

1415
use byteorder::{self, ByteOrder};
1516

@@ -38,10 +39,18 @@ pub enum TupleError {
3839
InvalidType { value: u8 },
3940
#[fail(display = "Invalid data")]
4041
InvalidData,
42+
#[fail(display = "UTF8 conversion error")]
43+
FromUtf8Error(FromUtf8Error),
4144
}
4245

4346
type Result<T> = std::result::Result<T, TupleError>;
4447

48+
impl From<FromUtf8Error> for TupleError {
49+
fn from(error: FromUtf8Error) -> Self {
50+
TupleError::FromUtf8Error(error)
51+
}
52+
}
53+
4554
trait SingleType: Copy {
4655
/// verifies the value matches this type
4756
fn expect(self, value: u8) -> Result<()>;
@@ -93,7 +102,8 @@ pub trait Single: Sized {
93102
fn encode_to_vec(&self) -> Vec<u8> {
94103
let mut v = Vec::new();
95104
// `self.encode` should not fail because undering `Write` does not return error.
96-
self.encode(&mut v).unwrap();
105+
self.encode(&mut v)
106+
.expect("single encoding should never fail");
97107
v
98108
}
99109

@@ -215,7 +225,7 @@ impl Single for String {
215225
STRING.expect(buf[0])?;
216226

217227
let (bytes, offset) = decode_bytes(&buf[1..])?;
218-
Ok((String::from_utf8(bytes).unwrap(), offset + 1))
228+
Ok((String::from_utf8(bytes)?, offset + 1))
219229
}
220230
}
221231

@@ -437,7 +447,8 @@ pub trait Tuple: Sized {
437447
fn encode<W: Write>(&self, _w: &mut W) -> std::io::Result<()>;
438448
fn encode_to_vec(&self) -> Vec<u8> {
439449
let mut v = Vec::new();
440-
self.encode(&mut v).unwrap();
450+
self.encode(&mut v)
451+
.expect("tuple encoding should never fail");
441452
v
442453
}
443454

0 commit comments

Comments
 (0)