Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions tests/symbol_length.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![cfg(all(unix, not(miri)))]

use std::io::{Error, Result, Write};

use pin_init::*;

#[pin_data]
pub struct Test {}

pub fn init() -> impl PinInit<Test, Error> {
pin_init!(Test {} ? Error)
}

fn init_fn_ptr<T, E, I: PinInit<T, E>>(_: &I) -> *mut () {
I::__pinned_init as *mut ()
}

fn read_symbols() -> Result<Vec<String>> {
let path = std::env::current_exe()?;
let output = std::process::Command::new("nm")
.arg(path)
.arg("--just-symbols")
.output()?;
if !output.status.success() {
Err(Error::other("exit code is not 0"))?
}
let symbols = String::from_utf8_lossy(&output.stdout);
Ok(symbols
.lines()
.filter(|x| !x.is_empty())
.map(|x| x.to_owned())
.collect())
}

#[test]
fn type_name() {
let init = init();
let init_fn = init_fn_ptr(&init);
std::hint::black_box(init_fn);

let mut symbols = match read_symbols() {
Ok(v) => v,
Err(err) => {
// This might be some setup issue (e.g. no nm installed).
// Instead of failing the test, print the error and continue.
// Do not use `eprintln!()` as it would be captured.
writeln!(std::io::stderr(), "cannot read symbols {:?}", err).unwrap();
return;
}
};

// Filter out non-related symbols.
symbols.retain(|x| x.contains("pin_init") && !x.contains("map"));

// Find the longest symbol
symbols.sort_by_key(|x| x.len().wrapping_neg());
let symbol = &symbols[0];
println!("{}: {}", symbol.len(), symbol);

assert!(symbol.len() < 180);
}
Loading