ui/autolayout/layoutexpression
선형 식. (원본 ui/autolayout/layoutexpression.js 의 LayoutExpression, 항 목록 + 상수항)
- 산술 함수는 모두 새 식을 돌려준다. (바뀌지 않는 값)
- equal_to, less_than_or_equal_to, greater_than_or_equal_to 는 LayoutConstraint 를 돌려준다. (required 강도)
- 인자는 숫자(f64, i32), LayoutVariable, LayoutTerm, LayoutExpression 을 받는다. (impl
Into<LayoutExpression>) - 원본과 다른 것:
- 원본 toExpression 의 실행 중 타입 검사는 From 구현으로 옮겼다. 받지 않는 타입은 컴파일이 막는다.
- multiply, divide 는 원본도 숫자만 받으므로 f64 만 받는다. (원본의 '스칼라만 지원' 예외는 생기지 않는다)
- 0 으로 나누기는 프로그램 실수라 원본과 같은 문구로 panic 한다.
- 원본은 LayoutConstraint 와 서로 import 하는 것을 피하려고 늦게 읽는 꾀를 쓴다. 러스트 판은 그냥 use 한다.
Linear expression. (LayoutExpression of the original ui/autolayout/layoutexpression.js, term list + constant)
- All arithmetic functions return a new expression. (immutable value)
- equal_to, less_than_or_equal_to, greater_than_or_equal_to return a LayoutConstraint. (required strength)
- Arguments take numbers (f64, i32), LayoutVariable, LayoutTerm, LayoutExpression. (impl
Into<LayoutExpression>) - Differences from the original:
- The runtime type check of the original toExpression moved into From implementations. Unaccepted types are stopped at compile time.
- multiply and divide take only f64, since the original also takes only numbers. (the original's 'scalars only' exception does not occur)
- Division by zero is a programming mistake, so it panics with the same message as the original.
- The original uses a late loading trick to avoid importing each other with LayoutConstraint. The Rust version just uses use.
LayoutExpressionstruct traits Clone, Debug, From<f64>, From<i32>, From<&LayoutVariable>, From<LayoutVariable>, From<LayoutTerm>, From<&LayoutTerm>, From<&LayoutExpression>
선형 식.
- 항(계수 * 변수) 목록과 상수항의 합이다. 같은 변수의 항도 합치지 않고 넣은 차례대로 둔다.
Linear expression.
- The sum of a list of terms (coefficient * variable) and a constant. Terms of the same variable are not merged and are kept in insertion order.
use vanilla::{LayoutExpression, LayoutVariable};
let left = LayoutVariable::new("left");
let width = LayoutVariable::new("width");
left.set_value(20.0);
width.set_value(100.0);
let center_x = LayoutExpression::from_variable(&left).add(LayoutExpression::from_variable(&width).divide(2.0));
assert_eq!(center_x.get_terms().len(), 2);
assert_eq!(center_x.get_value(), 70.0);
let shifted = center_x.subtract(10).multiply(2.0);
assert_eq!(shifted.get_constant(), -20.0);
assert_eq!(shifted.get_value(), 120.0);생성. (항 목록과 상수항)
Constructor. (term list and constant)
- termsVec<LayoutTerm>
- 항 목록.Term list.
- constantf64
- 상수항.Constant term.
반환ReturnsLayoutExpression 새 식.New expression.
짝Counterpart: vanilla.js LayoutExpression.constructor
변수 하나로 식 만들기. (1 * variable + 0)
Builds an expression from one variable. (1 * variable + 0)
- variable&LayoutVariable
- 식에 넣을 변수. (핸들을 복제해 쥔다)Variable to put in the expression. (holds a cloned handle)
반환ReturnsLayoutExpression 항 하나(1 * variable)와 상수항 0 의 식.An expression with one term (1 * variable) and constant 0.
짝Counterpart: vanilla.js LayoutExpression.fromVariable
상수만 가진 식 만들기.
Builds an expression with only a constant.
- valuef64
- 상수항.Constant term.
반환ReturnsLayoutExpression 항이 없고 상수항이 value 인 식.An expression with no terms and value as the constant.
짝Counterpart: vanilla.js LayoutExpression.fromConstant
받을 수 있는 입력을 식으로 바꾼다.
Turns an acceptable input into an expression.
- inputimpl Into<LayoutExpression>
- 바꿀 것. (숫자 f64, i32, LayoutVariable, LayoutTerm, LayoutExpression 과 그 참조)Thing to convert. (numbers f64, i32, LayoutVariable, LayoutTerm, LayoutExpression and their references)
반환ReturnsLayoutExpression 바꾼 식. (숫자는 상수 식, 변수는 1 * variable, 항은 그 항 하나, 식은 사본)The converted expression. (a number becomes a constant expression, a variable 1 * variable, a term just that term, an expression a copy)
짝Counterpart: vanilla.js LayoutExpression.toExpression
use vanilla::{LayoutExpression, LayoutTerm, LayoutVariable};
let x = LayoutVariable::new("x");
assert!(LayoutExpression::to_expression(12).is_constant());
assert_eq!(LayoutExpression::to_expression(2.5f64).get_constant(), 2.5);
assert_eq!(LayoutExpression::to_expression(&x).get_terms()[0].get_coefficient(), 1.0);
assert_eq!(LayoutExpression::to_expression(LayoutTerm::new(x.clone(), 3.0)).get_terms()[0].get_coefficient(), 3.0);항 목록 반환.
Returns the term list.
반환Returns&[LayoutTerm] 항 목록 조각. (넣은 차례)Slice of the term list. (insertion order)
짝Counterpart: vanilla.js LayoutExpression.getTerms
상수항 반환.
Returns the constant term.
반환Returnsf64 상수항.Constant term.
짝Counterpart: vanilla.js LayoutExpression.getConstant
항이 없는지. (상수항만 있는지)
Whether there are no terms. (whether there is only the constant)
반환Returnsbool 항이 없으면 true.true if there are no terms.
짝Counterpart: vanilla.js LayoutExpression.isConstant
평가 값 반환. (항 값의 합 + 상수항)
Returns the evaluated value. (sum of term values + constant)
반환Returnsf64 항마다 계수 * 변수의 지금 값을 더하고 상수항을 더한 값.The sum of coefficient * current variable value for each term, plus the constant.
짝Counterpart: vanilla.js LayoutExpression.getValue
덧셈.
Addition.
- inputimpl Into<LayoutExpression>
- 더할 것. (숫자, 변수, 항, 식)Thing to add. (number, variable, term, expression)
반환ReturnsLayoutExpression self + input 인 새 식. (self 의 항 뒤에 input 의 항을 붙이고 상수항을 더한다)A new expression self + input. (appends input's terms after self's terms and adds the constants)
짝Counterpart: vanilla.js LayoutExpression.add
뺄셈.
Subtraction.
- inputimpl Into<LayoutExpression>
- 뺄 것. (숫자, 변수, 항, 식)Thing to subtract. (number, variable, term, expression)
반환ReturnsLayoutExpression self - input 인 새 식. (input 에 -1 을 곱해 더한다)A new expression self - input. (multiplies input by -1 and adds it)
짝Counterpart: vanilla.js LayoutExpression.subtract
스칼라 곱셈. (변수끼리의 곱 같은 비선형 곱은 받지 않는다)
Scalar multiplication. (nonlinear products such as multiplying variables together are not accepted)
- coefficientf64
- 곱할 수.Number to multiply by.
반환ReturnsLayoutExpression 모든 계수와 상수항에 coefficient 를 곱한 새 식.A new expression with every coefficient and the constant multiplied by coefficient.
짝Counterpart: vanilla.js LayoutExpression.multiply
스칼라 나눗셈. (0 으로 나누면 panic 한다)
Scalar division. (panics on division by zero)
- denominatorf64
- 나눌 수. (0 이면 panic)Number to divide by. (panics if 0)
반환ReturnsLayoutExpression self * (1 / denominator) 인 새 식.A new expression self * (1 / denominator).
짝Counterpart: vanilla.js LayoutExpression.divide
같음 제약 만들기. (self == input)
Creates an equality constraint. (self == input)
- inputimpl Into<LayoutExpression>
- 맞출 것. (숫자, 변수, 항, 식)Thing to match. (number, variable, term, expression)
반환ReturnsLayoutConstraint 식이 self - input 이고 관계가 Equal 인 required 제약. (솔버에는 걸지 않는다)A required constraint whose expression is self - input and whose relation is Equal. (not added to the solver)
짝Counterpart: vanilla.js LayoutExpression.equalTo
use vanilla::{LayoutExpression, LayoutSolver, LayoutVariable};
let solver = LayoutSolver::new();
let left = LayoutVariable::new("left");
let right = LayoutVariable::new("right");
solver.add_constraint(&LayoutExpression::from_variable(&left).equal_to(30)).unwrap();
let right_constraint = LayoutExpression::from_variable(&right).equal_to(LayoutExpression::from_variable(&left).add(200));
solver.add_constraint(&right_constraint).unwrap();
solver.update_variables();
assert!((right.get_value() - 230.0).abs() < 1e-9);pub fn less_than_or_equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint#작거나 같음 제약 만들기. (self <= input)
Creates a less than or equal constraint. (self <= input)
- inputimpl Into<LayoutExpression>
- 넘지 않을 것. (숫자, 변수, 항, 식)Thing not to exceed. (number, variable, term, expression)
반환ReturnsLayoutConstraint 식이 self - input 이고 관계가 LessThanOrEqual 인 required 제약.A required constraint whose expression is self - input and whose relation is LessThanOrEqual.
짝Counterpart: vanilla.js LayoutExpression.lessThanOrEqualTo
pub fn greater_than_or_equal_to(&self, input: impl Into<LayoutExpression>) -> LayoutConstraint#크거나 같음 제약 만들기. (self >= input)
Creates a greater than or equal constraint. (self >= input)
- inputimpl Into<LayoutExpression>
- 밑돌지 않을 것. (숫자, 변수, 항, 식)Thing not to fall below. (number, variable, term, expression)
반환ReturnsLayoutConstraint 식이 self - input 이고 관계가 GreaterThanOrEqual 인 required 제약.A required constraint whose expression is self - input and whose relation is GreaterThanOrEqual.
짝Counterpart: vanilla.js LayoutExpression.greaterThanOrEqualTo