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

Extend smol_str usage #6463

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions api/rs/build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ i-slint-compiler = { workspace = true, features = ["default", "rust", "display-d
spin_on = { workspace = true }
thiserror = "1"
toml_edit = { workspace = true }
smol_str = "0.3.1"
milianw marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion api/rs/build/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use std::collections::HashMap;
use std::env;
use std::io::{BufWriter, Write};
use std::path::Path;
use smol_str::SmolStr;

use i_slint_compiler::diagnostics::BuildDiagnostics;

Expand Down Expand Up @@ -426,7 +427,7 @@ pub fn compile_with_output(
}

let mut compiler_config = config.config;
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").ok();
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").as_ref().map(|s| SmolStr::new(s.as_str())).ok();

let syntax_node = syntax_node.expect("diags contained no compilation errors");

Expand Down
1 change: 1 addition & 0 deletions api/rs/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ i-slint-compiler = { workspace = true, features = ["default", "proc_macro_span",
proc-macro2 = "1.0.17"
quote = "1.0"
spin_on = { workspace = true }
smol_str = "0.3.1"
3 changes: 2 additions & 1 deletion api/rs/macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use i_slint_compiler::parser::SyntaxKind;
use i_slint_compiler::*;
use proc_macro::{Spacing, TokenStream, TokenTree};
use quote::quote;
use smol_str::SmolStr;

/// Returns true if the two token are touching. For example the two token `foo`and `-` are touching if
/// it was written like so in the source code: `foo-` but not when written like so `foo -`
Expand Down Expand Up @@ -407,7 +408,7 @@ pub fn slint(stream: TokenStream) -> TokenStream {
}

//println!("{:#?}", syntax_node);
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").ok();
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").as_ref().map(|s| SmolStr::new(s.as_str())).ok();
let (root_component, diag, loader) =
spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config));
//println!("{:#?}", tree);
Expand Down
9 changes: 5 additions & 4 deletions internal/compiler/builtin_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::expression_tree::{
};
use crate::langtype::{EnumerationValue, Type};
use crate::parser::NodeOrToken;
use smol_str::{format_smolstr, ToSmolStr};

/// Used for uniquely name some variables
static COUNTER: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(1);
Expand Down Expand Up @@ -371,7 +372,7 @@ fn to_debug_string(
),
op: '+',
rhs: Box::new(Expression::StringLiteral(
Type::UnitProduct(ty.as_unit_product().unwrap()).to_string(),
Type::UnitProduct(ty.as_unit_product().unwrap()).to_smolstr(),
)),
},
Type::Bool => Expression::Condition {
Expand All @@ -380,14 +381,14 @@ fn to_debug_string(
false_expr: Box::new(Expression::StringLiteral("false".into())),
},
Type::Struct { fields, .. } => {
let local_object = format!(
let local_object = format_smolstr!(
"debug_struct{}",
COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
);
let mut string = None;
for k in fields.keys() {
let field_name =
if string.is_some() { format!(", {}: ", k) } else { format!("{{ {}: ", k) };
if string.is_some() { format_smolstr!(", {}: ", k) } else { format_smolstr!("{{ {}: ", k) };
let value = to_debug_string(
Expression::StructFieldAccess {
base: Box::new(Expression::ReadLocalVariable {
Expand Down Expand Up @@ -431,7 +432,7 @@ fn to_debug_string(
name: local_object.into(),
value: Box::new(expr),
}];
let mut cond = Expression::StringLiteral(format!("Error: invalid value for {}", ty));
let mut cond = Expression::StringLiteral(format_smolstr!("Error: invalid value for {}", ty));
for (idx, val) in enu.values.iter().enumerate() {
cond = Expression::Condition {
condition: Box::new(Expression::BinaryExpression {
Expand Down
39 changes: 20 additions & 19 deletions internal/compiler/expression_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use core::cell::RefCell;
use std::cell::Cell;
use std::collections::HashMap;
use std::rc::{Rc, Weak};
use smol_str::SmolStr;

// FIXME remove the pub
pub use crate::namedreference::NamedReference;
Expand Down Expand Up @@ -44,7 +45,7 @@ pub enum BuiltinFunction {
ClosePopupWindow,
SetSelectionOffsets,
/// A function that belongs to an item (such as TextInput's select-all function).
ItemMemberFunction(String),
ItemMemberFunction(SmolStr),
ItemFontMetrics,
/// the "42".to_float()
StringToFloat,
Expand Down Expand Up @@ -185,10 +186,10 @@ impl BuiltinFunction {
BuiltinFunction::ColorRgbaStruct => Type::Function {
return_type: Box::new(Type::Struct {
fields: IntoIterator::into_iter([
("red".to_string(), Type::Int32),
("green".to_string(), Type::Int32),
("blue".to_string(), Type::Int32),
("alpha".to_string(), Type::Int32),
(SmolStr::new_static("red"), Type::Int32),
(SmolStr::new_static("green"), Type::Int32),
(SmolStr::new_static("blue"), Type::Int32),
(SmolStr::new_static("alpha"), Type::Int32),
])
.collect(),
name: Some("Color".into()),
Expand All @@ -200,10 +201,10 @@ impl BuiltinFunction {
BuiltinFunction::ColorHsvaStruct => Type::Function {
return_type: Box::new(Type::Struct {
fields: IntoIterator::into_iter([
("hue".to_string(), Type::Float32),
("saturation".to_string(), Type::Float32),
("value".to_string(), Type::Float32),
("alpha".to_string(), Type::Float32),
(SmolStr::new_static("hue"), Type::Float32),
(SmolStr::new_static("saturation"), Type::Float32),
(SmolStr::new_static("value"), Type::Float32),
(SmolStr::new_static("alpha"), Type::Float32),
])
.collect(),
name: Some("Color".into()),
Expand Down Expand Up @@ -235,11 +236,11 @@ impl BuiltinFunction {
BuiltinFunction::ImageSize => Type::Function {
return_type: Box::new(Type::Struct {
fields: IntoIterator::into_iter([
("width".to_string(), Type::Int32),
("height".to_string(), Type::Int32),
(SmolStr::new_static("width"), Type::Int32),
(SmolStr::new_static("height"), Type::Int32),
])
.collect(),
name: Some("Size".to_string()),
name: Some("Size".into()),
node: None,
rust_attributes: None,
}),
Expand Down Expand Up @@ -576,7 +577,7 @@ pub enum Expression {
Uncompiled(SyntaxNode),

/// A string literal. The .0 is the content of the string, without the quotes
StringLiteral(String),
StringLiteral(SmolStr),
/// Number
NumberLiteral(f64, Unit),
/// Bool
Expand Down Expand Up @@ -636,22 +637,22 @@ pub enum Expression {

/// Should be directly within a CodeBlock expression, and store the value of the expression in a local variable
StoreLocalVariable {
name: String,
name: SmolStr,
value: Box<Expression>,
},

/// a reference to the local variable with the given name. The type system should ensure that a variable has been stored
/// with this name and this type before in one of the statement of an enclosing codeblock
ReadLocalVariable {
name: String,
name: SmolStr,
ty: Type,
},

/// Access to a field of the given name within a struct.
StructFieldAccess {
/// This expression should have [`Type::Struct`] type
base: Box<Expression>,
name: String,
name: SmolStr,
},

/// Access to a index within an array.
Expand Down Expand Up @@ -717,7 +718,7 @@ pub enum Expression {
},
Struct {
ty: Type,
values: HashMap<String, Expression>,
values: HashMap<SmolStr, Expression>,
},

PathData(Path),
Expand Down Expand Up @@ -1367,7 +1368,7 @@ impl Expression {
| Type::LayoutCache => Expression::Invalid,
Type::Void => Expression::CodeBlock(vec![]),
Type::Float32 => Expression::NumberLiteral(0., Unit::None),
Type::String => Expression::StringLiteral(String::new()),
Type::String => Expression::StringLiteral(SmolStr::default()),
Type::Int32 | Type::Color | Type::UnitProduct(_) => Expression::Cast {
from: Box::new(Expression::NumberLiteral(0., Unit::None)),
to: ty.clone(),
Expand Down Expand Up @@ -1640,7 +1641,7 @@ pub enum EasingCurve {
#[derive(Clone, Debug)]
pub enum ImageReference {
None,
AbsolutePath(String),
AbsolutePath(SmolStr),
EmbeddedData { resource_id: usize, extension: String },
EmbeddedTexture { resource_id: usize },
}
Expand Down
Loading
Loading