mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 13:13:44 +01:00
cargo fmt; fix line length warnings
This commit is contained in:
parent
90daad999e
commit
6be75bd61d
@ -13,7 +13,6 @@ use tor_allocate::allocate_and_copy_string;
|
||||
/// Translate C enums to Rust Proto enums, using the integer value of the C
|
||||
/// enum to map to its associated Rust enum
|
||||
/// This is dependant on the associated C enum preserving ordering.
|
||||
/// Modify the C documentation to give warnings- you must also re-order the rust
|
||||
fn translate_to_rust(c_proto: uint32_t) -> Result<Proto, &'static str> {
|
||||
match c_proto {
|
||||
0 => Ok(Proto::Link),
|
||||
|
@ -142,7 +142,8 @@ fn tor_supported() -> Result<HashMap<Proto, HashSet<u32>>, &'static str> {
|
||||
/// This function will error if:
|
||||
///
|
||||
/// * the `version_string` is empty or contains an equals (`"="`) sign,
|
||||
/// * the expansion of a version range produces an error (see `expand_version_range`),
|
||||
/// * the expansion of a version range produces an error (see
|
||||
/// `expand_version_range`),
|
||||
/// * any single version number is not parseable as an `u32` in radix 10, or
|
||||
/// * there are greater than 2^16 version numbers to expand.
|
||||
///
|
||||
@ -293,10 +294,12 @@ pub fn all_supported(protocols: &str) -> (bool, String) {
|
||||
/// ```
|
||||
/// use protover::*;
|
||||
///
|
||||
/// let is_supported = protover_string_supports_protocol("Link=3-4 Cons=1", Proto::Cons,1);
|
||||
/// let is_supported = protover_string_supports_protocol("Link=3-4 Cons=1",
|
||||
/// Proto::Cons,1);
|
||||
/// assert_eq!(true, is_supported);
|
||||
///
|
||||
/// let is_not_supported = protover_string_supports_protocol("Link=3-4 Cons=1", Proto::Cons,5);
|
||||
/// let is_not_supported = protover_string_supports_protocol("Link=3-4 Cons=1",
|
||||
/// Proto::Cons,5);
|
||||
/// assert_eq!(false, is_not_supported)
|
||||
/// ```
|
||||
pub fn protover_string_supports_protocol(
|
||||
@ -363,7 +366,7 @@ fn expand_version_range(range: &str) -> Result<Vec<u32>, &'static str> {
|
||||
))?;
|
||||
|
||||
// We can use inclusive range syntax when it becomes stable.
|
||||
Ok((lower..higher+1).collect())
|
||||
Ok((lower..higher + 1).collect())
|
||||
}
|
||||
|
||||
/// Checks to see if there is a continuous range of integers, starting at the
|
||||
@ -477,8 +480,7 @@ fn contract_protocol_list<'a>(supported_set: &'a HashSet<u32>) -> String {
|
||||
fn parse_protocols_from_string_with_no_validation<'a>(
|
||||
protocol_string: &'a str,
|
||||
) -> Result<HashMap<String, HashSet<u32>>, &'static str> {
|
||||
let protocols = &protocol_string.split(" ")
|
||||
.collect::<Vec<&'a str>>()[..];
|
||||
let protocols = &protocol_string.split(" ").collect::<Vec<&'a str>>()[..];
|
||||
|
||||
let mut parsed: HashMap<String, HashSet<u32>> = HashMap::new();
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
extern crate protover;
|
||||
|
||||
#[test]
|
||||
fn parse_protocol_list_with_single_protocol_and_single_version_returns_set_of_one(){
|
||||
fn parse_protocol_list_with_single_proto_and_single_version() {
|
||||
let protocol = "Cons=1";
|
||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
||||
assert_eq!(true, is_supported);
|
||||
@ -9,7 +9,7 @@ fn parse_protocol_list_with_single_protocol_and_single_version_returns_set_of_on
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_protocol_list_with_single_protocol_and_multiple_versions_returns_set_of_one(){
|
||||
fn parse_protocol_list_with_single_protocol_and_multiple_versions() {
|
||||
let protocol = "Cons=1-2";
|
||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
||||
assert_eq!(true, is_supported);
|
||||
@ -17,7 +17,7 @@ fn parse_protocol_list_with_single_protocol_and_multiple_versions_returns_set_of
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_protocol_list_with_different_single_protocol_and_single_version_returns_set_of_one(){
|
||||
fn parse_protocol_list_with_different_single_protocol_and_single_version() {
|
||||
let protocol = "HSDir=1";
|
||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
||||
assert_eq!(true, is_supported);
|
||||
@ -25,7 +25,7 @@ fn parse_protocol_list_with_different_single_protocol_and_single_version_returns
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_protocol_list_with_single_protocol_and_supported_version_returns_set_of_one(){
|
||||
fn parse_protocol_list_with_single_protocol_and_supported_version() {
|
||||
let protocol = "Desc=2";
|
||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
||||
assert_eq!(true, is_supported);
|
||||
@ -33,7 +33,7 @@ fn parse_protocol_list_with_single_protocol_and_supported_version_returns_set_of
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_protocol_list_with_two_protocols_and_single_version_returns_set_of_one(){
|
||||
fn parse_protocol_list_with_two_protocols_and_single_version() {
|
||||
let protocols = "Cons=1 HSDir=1";
|
||||
let (is_supported, unsupported) = protover::all_supported(protocols);
|
||||
assert_eq!(true, is_supported);
|
||||
@ -42,7 +42,7 @@ fn parse_protocol_list_with_two_protocols_and_single_version_returns_set_of_one(
|
||||
|
||||
|
||||
#[test]
|
||||
fn parse_protocol_list_with_single_protocol_and_two_nonsequential_versions_returns_set_of_two(){
|
||||
fn parse_protocol_list_with_single_protocol_and_two_nonsequential_versions() {
|
||||
let protocol = "Desc=1,2";
|
||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
||||
assert_eq!(true, is_supported);
|
||||
@ -51,7 +51,7 @@ fn parse_protocol_list_with_single_protocol_and_two_nonsequential_versions_retur
|
||||
|
||||
|
||||
#[test]
|
||||
fn parse_protocol_list_with_single_protocol_and_two_sequential_versions_returns_set_of_two(){
|
||||
fn parse_protocol_list_with_single_protocol_and_two_sequential_versions() {
|
||||
let protocol = "Desc=1-2";
|
||||
let (is_supported, unsupported) = protover::all_supported(protocol);
|
||||
assert_eq!(true, is_supported);
|
||||
@ -169,7 +169,7 @@ fn protover_string_supports_protocol_returns_false_for_single_unsupported() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn protover_string_supports_protocol_returns_false_when_protocol_name_is_not_in_map(){
|
||||
fn protover_string_supports_protocol_returns_false_for_unsupported() {
|
||||
let protocols = "Link=3-4";
|
||||
let is_supported = protover::protover_string_supports_protocol(
|
||||
protocols,
|
||||
|
@ -30,9 +30,8 @@ impl Smartlist<String> for Stringlist {
|
||||
|
||||
// unsafe, as we need to extract the smartlist list into a vector of
|
||||
// pointers, and then transform each element into a Rust string.
|
||||
let elems: &[*const i8] = unsafe {
|
||||
slice::from_raw_parts(self.list, self.num_used as usize)
|
||||
};
|
||||
let elems: &[*const i8] =
|
||||
unsafe { slice::from_raw_parts(self.list, self.num_used as usize) };
|
||||
|
||||
for elem in elems.iter() {
|
||||
if elem.is_null() {
|
||||
|
@ -3,13 +3,13 @@ use std::{ptr, slice, mem};
|
||||
|
||||
#[cfg(not(test))]
|
||||
extern "C" {
|
||||
fn tor_malloc_ ( size: usize) -> *mut c_void;
|
||||
fn tor_malloc_(size: usize) -> *mut c_void;
|
||||
}
|
||||
|
||||
// Defined only for tests, used for testing purposes, so that we don't need
|
||||
// to link to tor C files. Uses the system allocator
|
||||
#[cfg(test)]
|
||||
extern "C" fn tor_malloc_ ( size: usize) -> *mut c_void {
|
||||
extern "C" fn tor_malloc_(size: usize) -> *mut c_void {
|
||||
use libc::malloc;
|
||||
unsafe { malloc(size) }
|
||||
}
|
||||
@ -32,7 +32,7 @@ extern "C" fn tor_malloc_ ( size: usize) -> *mut c_void {
|
||||
pub fn allocate_and_copy_string(src: &String) -> *mut c_char {
|
||||
let bytes: &[u8] = src.as_bytes();
|
||||
|
||||
let size = mem::size_of_val::<[u8]>(bytes);
|
||||
let size = mem::size_of_val::<[u8]>(bytes);
|
||||
let size_one_byte = mem::size_of::<u8>();
|
||||
|
||||
// handle integer overflow when adding one to the calculated length
|
||||
@ -51,7 +51,7 @@ pub fn allocate_and_copy_string(src: &String) -> *mut c_char {
|
||||
|
||||
// set the last byte as null, using the ability to index into a slice
|
||||
// rather than doing pointer arithmatic
|
||||
let slice = unsafe { slice::from_raw_parts_mut(dest, size_with_null_byte)};
|
||||
let slice = unsafe { slice::from_raw_parts_mut(dest, size_with_null_byte) };
|
||||
slice[size] = 0; // add a null terminator
|
||||
|
||||
dest as *mut c_char
|
||||
@ -70,9 +70,8 @@ mod test {
|
||||
let empty = String::new();
|
||||
let allocated_empty = allocate_and_copy_string(&empty);
|
||||
|
||||
let allocated_empty_rust = unsafe {
|
||||
CStr::from_ptr(allocated_empty).to_str().unwrap()
|
||||
};
|
||||
let allocated_empty_rust =
|
||||
unsafe { CStr::from_ptr(allocated_empty).to_str().unwrap() };
|
||||
|
||||
assert_eq!("", allocated_empty_rust);
|
||||
|
||||
@ -89,9 +88,8 @@ mod test {
|
||||
let empty = String::from("foo bar biz");
|
||||
let allocated_empty = allocate_and_copy_string(&empty);
|
||||
|
||||
let allocated_empty_rust = unsafe {
|
||||
CStr::from_ptr(allocated_empty).to_str().unwrap()
|
||||
};
|
||||
let allocated_empty_rust =
|
||||
unsafe { CStr::from_ptr(allocated_empty).to_str().unwrap() };
|
||||
|
||||
assert_eq!("foo bar biz", allocated_empty_rust);
|
||||
|
||||
|
@ -15,7 +15,9 @@ use tor_allocate::allocate_and_copy_string;
|
||||
/// ```
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rust_welcome_string() -> *mut c_char {
|
||||
let rust_welcome = String::from("Tor is running with Rust integration. Please report \
|
||||
any bugs you encouter.");
|
||||
let rust_welcome = String::from(
|
||||
"Tor is running with Rust integration. Please report \
|
||||
any bugs you encouter.",
|
||||
);
|
||||
allocate_and_copy_string(&rust_welcome)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user