Skip to content

Commit

Permalink
feat: Added convenience api for addr
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 25, 2018
1 parent fa695b3 commit f564fe4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/protocol/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,36 @@ impl fmt::Display for Addr {
}
}

impl From<u64> for Addr {
fn from(addr: u64) -> Addr {
Addr(addr)
}
}

impl From<u32> for Addr {
fn from(addr: u32) -> Addr {
Addr(addr as u64)
}
}

impl From<usize> for Addr {
fn from(addr: usize) -> Addr {
Addr(addr as u64)
}
}

impl<T> From<*const T> for Addr {
fn from(addr: *const T) -> Addr {
Addr(addr as u64)
}
}

impl Into<u64> for Addr {
fn into(self: Addr) -> u64 {
self.0
}
}

/// Represents a single thread.
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
#[serde(default)]
Expand Down
8 changes: 8 additions & 0 deletions tests/test_protocol_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,14 @@ fn test_addr_format() {
assert_eq!(serde_json::from_str::<v7::Addr>("\"0X2A\"").unwrap(), v7::Addr(42));
}

#[test]
fn test_addr_api() {
use std::ptr;
assert_eq!(v7::Addr::from(42u64), v7::Addr(42));
assert_eq!(v7::Addr::from(42u32), v7::Addr(42));
assert_eq!(v7::Addr::from(ptr::null::<()>()), v7::Addr(0));
}

#[test]
fn test_thread_id_format() {
assert_eq!(serde_json::to_string(&v7::ThreadId::Int(0)).unwrap(), "0");
Expand Down

0 comments on commit f564fe4

Please sign in to comment.