Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tabled_derive/ Fix issue with Cow usage #397

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions json_to_table/src/table/collapsed_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn _collapsed_table(val: &Value, cfg: &Config, dims: &Dimensions, ctx: PrintCont
}

fn generate_vertical_array(
list: &Vec<Value>,
list: &[Value],
cfg: &Config,
dims: &Dimensions,
ctx: PrintContext,
Expand Down Expand Up @@ -178,7 +178,7 @@ fn generate_vertical_array(
}

fn generate_horizontal_array(
list: &Vec<Value>,
list: &[Value],
cfg: &Config,
dims: &Dimensions,
ctx: PrintContext,
Expand Down
2 changes: 1 addition & 1 deletion papergrid/src/records/vec_records/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<T> VecRecords<T> {
///
/// It assumes that data vector has all rows has the same length().
pub fn new(data: Vec<Vec<T>>) -> Self {
let count_columns = data.get(0).map_or(0, |row| row.len());
let count_columns = data.first().map_or(0, |row| row.len());
let count_rows = data.len();
let shape = (count_rows, count_columns);

Expand Down
4 changes: 2 additions & 2 deletions table_to_html/tests/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn html_element_visitor() {

#[test]
fn html_element_visitor_mut() {
struct Visitor(usize);
struct Visitor;

impl HtmlVisitorMut for Visitor {
fn visit_element_mut(&mut self, e: &mut HtmlElement) -> bool {
Expand Down Expand Up @@ -108,7 +108,7 @@ fn html_element_visitor_mut() {
)])),
);

let mut visitor = Visitor(0);
let mut visitor = Visitor;
table.visit_mut(&mut visitor);

assert_table!(
Expand Down
2 changes: 1 addition & 1 deletion tabled/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ macros = ["std"]

[dependencies]
papergrid = { version = "0.11", default-features = false }
tabled_derive = { version = "0.7", optional = true }
tabled_derive = { path = "../tabled_derive", optional = true }
ansi-str = { version = "0.8", optional = true }
ansitok = { version = "0.2", optional = true }
unicode-width = "0.1"
Expand Down
8 changes: 4 additions & 4 deletions tabled/src/grid/records/resizable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ where
}

fn push_row(&mut self) {
let count_columns = self.get(0).map(|l| l.len()).unwrap_or(0);
let count_columns = self.first().map(|l| l.len()).unwrap_or(0);
self.push(vec![T::default(); count_columns]);
}

Expand All @@ -115,7 +115,7 @@ where
}

fn insert_row(&mut self, row: usize) {
let count_columns = self.get(0).map(|l| l.len()).unwrap_or(0);
let count_columns = self.first().map(|l| l.len()).unwrap_or(0);
self.insert(row, vec![T::default(); count_columns]);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ where
let records = std::mem::replace(self, VecRecords::new(vec![]));
let mut data: Vec<Vec<_>> = records.into();

let count_columns = data.get(0).map(|l| l.len()).unwrap_or(0);
let count_columns = data.first().map(|l| l.len()).unwrap_or(0);
data.push(vec![T::default(); count_columns]);

*self = VecRecords::new(data);
Expand Down Expand Up @@ -198,7 +198,7 @@ where
let records = std::mem::replace(self, VecRecords::new(vec![]));
let mut data: Vec<Vec<_>> = records.into();

let count_columns = data.get(0).map(|l| l.len()).unwrap_or(0);
let count_columns = data.first().map(|l| l.len()).unwrap_or(0);
data.insert(row, vec![T::default(); count_columns]);

*self = VecRecords::new(data);
Expand Down
4 changes: 2 additions & 2 deletions tabled/src/tables/table_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ mod print {
}

fn generate_table_column(
list: &Vec<TableValue>,
list: &[TableValue],
cfg: &CompactMultilineConfig,
dims: &Dimensions,
priority: PoolTableDimension,
Expand Down Expand Up @@ -444,7 +444,7 @@ mod print {
}

fn generate_table_row(
list: &Vec<TableValue>,
list: &[TableValue],
cfg: &CompactMultilineConfig,
dims: &Dimensions,
priority: PoolTableDimension,
Expand Down
9 changes: 9 additions & 0 deletions tabled/tests/derive/derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,15 @@ fn test_skip_enum_0() {
assert_eq!(Letters::Digit.fields(), vec!["", ""]);
}

#[test]
fn test_display_with_2() {
#[derive(tabled::Tabled)]
struct Struct<'a> {
#[tabled(display_with("std::path::Path::display"))]
path: &'a std::path::Path,
}
}

mod __ {
#[test]
fn dont_import_the_trait() {
Expand Down
8 changes: 4 additions & 4 deletions tabled_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ fn info_from_variant(
},
};

let call = match args {
let result = match args {
Some(args) => use_function(&args, func),
None => use_function_no_args(func),
};

quote! { ::std::borrow::Cow::from(#call) }
quote! { ::std::borrow::Cow::from(format!("{}", #result)) }
} else {
let default_value = "+";
quote! { ::std::borrow::Cow::Borrowed(#default_value) }
Expand Down Expand Up @@ -440,12 +440,12 @@ fn get_field_fields(field: &TokenStream, attr: &Attributes) -> TokenStream {
},
};

let call = match args {
let result = match args {
Some(args) => use_function(&args, func),
None => use_function_no_args(func),
};

return quote!(vec![::std::borrow::Cow::from(#call)]);
return quote!(vec![::std::borrow::Cow::from(format!("{}", #result))]);
}

quote!(vec![::std::borrow::Cow::Owned(format!("{}", #field))])
Expand Down
Loading