Skip to content

Commit a10b987

Browse files
Add aliases to
* Add `set_ne` aliases to `ActiveValue` * Doc comments supplement
1 parent c7439a3 commit a10b987

2 files changed

Lines changed: 50 additions & 18 deletions

File tree

sea-orm-sync/src/entity/active_value.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ where
6060
///
6161
/// Use this to insert or set a specific value.
6262
///
63-
/// When editing an existing value, you can use [set_if_not_equals][ActiveValue::set_if_not_equals]
63+
/// When editing an existing value, you can use [set_ne][ActiveValue::set_ne]
6464
/// to preserve the [Unchanged] state when the new value is the same as the old one.
6565
/// Then you can meaningfully use methods like [ActiveModelTrait::is_changed].
6666
Set(V),
@@ -69,7 +69,7 @@ where
6969
/// You get these when you query an existing [Model][crate::ModelTrait]
7070
/// from the database and convert it into an [ActiveModel][ActiveModelTrait].
7171
///
72-
/// When you edit it, you can use [set_if_not_equals][ActiveValue::set_if_not_equals]
72+
/// When you edit it, you can use [set_ne][ActiveValue::set_ne]
7373
/// to preserve this "unchanged" state if the new value is the same as the old one.
7474
/// Then you can meaningfully use methods like [ActiveModelTrait::is_changed].
7575
Unchanged(V),
@@ -325,15 +325,15 @@ where
325325
/// let mut value = ActiveValue::Unchanged("old");
326326
///
327327
/// // This wouldn't be the case if we used plain `value = Set("old");`
328-
/// value.set_if_not_equals("old");
328+
/// value.set_ne("old");
329329
/// assert!(value.is_unchanged());
330330
///
331331
/// // Only when we change the actual `&str` value, it becomes `Set`
332-
/// value.set_if_not_equals("new");
332+
/// value.set_ne("new");
333333
/// assert_eq!(value.is_unchanged(), false);
334334
/// assert_eq!(value, ActiveValue::Set("new"));
335335
/// ```
336-
pub fn set_if_not_equals(&mut self, value: V)
336+
pub fn set_ne(&mut self, value: V)
337337
where
338338
V: PartialEq,
339339
{
@@ -343,11 +343,19 @@ where
343343
}
344344
}
345345

346+
/// Alias for [`ActiveValue::set_ne`]. Kept for compatibility.
347+
pub fn set_if_not_equals(&mut self, value: V)
348+
where
349+
V: PartialEq,
350+
{
351+
self.set_ne(value);
352+
}
353+
346354
/// `Set(value)`, except when [`self.is_unchanged()`][ActiveValue#method.is_unchanged],
347355
/// `value` equals the current [Unchanged][ActiveValue::Unchanged] value, and `value`
348356
/// does not match a given predicate.
349357
///
350-
/// This is useful in the same situations as [ActiveValue#method.set_if_not_equals] as
358+
/// This is useful in the same situations as [`ActiveValue::set_ne`] as
351359
/// well as when you want to leave an existing [Set][ActiveValue::Set] value alone
352360
/// depending on a condition, such as ensuring a `None` value never replaced an
353361
/// existing `Some` value. This can come up when trying to merge two [ActiveValue]s.
@@ -360,14 +368,14 @@ where
360368
/// let mut value = ActiveValue::Set(Some("old"));
361369
///
362370
/// // since Option::is_some(None) == false, we leave the existing set value alone
363-
/// value.set_if_not_equals_and(None, Option::is_some);
371+
/// value.set_ne_and(None, Option::is_some);
364372
/// assert_eq!(value, ActiveValue::Set(Some("old")));
365373
///
366374
/// // since Option::is_some(Some("new")) == true, we replace the set value
367-
/// value.set_if_not_equals_and(Some("new"), Option::is_some);
375+
/// value.set_ne_and(Some("new"), Option::is_some);
368376
/// assert_eq!(value, ActiveValue::Set(Some("new")));
369377
/// ```
370-
pub fn set_if_not_equals_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)
378+
pub fn set_ne_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)
371379
where
372380
V: PartialEq,
373381
{
@@ -378,6 +386,14 @@ where
378386
}
379387
}
380388

389+
/// Alias for [`ActiveValue::set_ne_and`]. Kept for compatibility.
390+
pub fn set_if_not_equals_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)
391+
where
392+
V: PartialEq,
393+
{
394+
self.set_ne_and(value, f);
395+
}
396+
381397
/// Get the inner value, unless `self` is [NotSet][ActiveValue::NotSet].
382398
///
383399
/// There's also a panicking version: [ActiveValue::as_ref].

src/entity/active_value.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ where
6060
///
6161
/// Use this to insert or set a specific value.
6262
///
63-
/// When editing an existing value, you can use [set_if_not_equals][ActiveValue::set_if_not_equals]
63+
/// When editing an existing value, you can use [set_ne][ActiveValue::set_ne]
6464
/// to preserve the [Unchanged] state when the new value is the same as the old one.
6565
/// Then you can meaningfully use methods like [ActiveModelTrait::is_changed].
6666
Set(V),
@@ -69,7 +69,7 @@ where
6969
/// You get these when you query an existing [Model][crate::ModelTrait]
7070
/// from the database and convert it into an [ActiveModel][ActiveModelTrait].
7171
///
72-
/// When you edit it, you can use [set_if_not_equals][ActiveValue::set_if_not_equals]
72+
/// When you edit it, you can use [set_ne][ActiveValue::set_ne]
7373
/// to preserve this "unchanged" state if the new value is the same as the old one.
7474
/// Then you can meaningfully use methods like [ActiveModelTrait::is_changed].
7575
Unchanged(V),
@@ -325,15 +325,15 @@ where
325325
/// let mut value = ActiveValue::Unchanged("old");
326326
///
327327
/// // This wouldn't be the case if we used plain `value = Set("old");`
328-
/// value.set_if_not_equals("old");
328+
/// value.set_ne("old");
329329
/// assert!(value.is_unchanged());
330330
///
331331
/// // Only when we change the actual `&str` value, it becomes `Set`
332-
/// value.set_if_not_equals("new");
332+
/// value.set_ne("new");
333333
/// assert_eq!(value.is_unchanged(), false);
334334
/// assert_eq!(value, ActiveValue::Set("new"));
335335
/// ```
336-
pub fn set_if_not_equals(&mut self, value: V)
336+
pub fn set_ne(&mut self, value: V)
337337
where
338338
V: PartialEq,
339339
{
@@ -343,11 +343,19 @@ where
343343
}
344344
}
345345

346+
/// Alias for [`ActiveValue::set_ne`]. Kept for compatibility.
347+
pub fn set_if_not_equals(&mut self, value: V)
348+
where
349+
V: PartialEq,
350+
{
351+
self.set_ne(value);
352+
}
353+
346354
/// `Set(value)`, except when [`self.is_unchanged()`][ActiveValue#method.is_unchanged],
347355
/// `value` equals the current [Unchanged][ActiveValue::Unchanged] value, and `value`
348356
/// does not match a given predicate.
349357
///
350-
/// This is useful in the same situations as [ActiveValue#method.set_if_not_equals] as
358+
/// This is useful in the same situations as [`ActiveValue::set_ne`] as
351359
/// well as when you want to leave an existing [Set][ActiveValue::Set] value alone
352360
/// depending on a condition, such as ensuring a `None` value never replaced an
353361
/// existing `Some` value. This can come up when trying to merge two [ActiveValue]s.
@@ -360,14 +368,14 @@ where
360368
/// let mut value = ActiveValue::Set(Some("old"));
361369
///
362370
/// // since Option::is_some(None) == false, we leave the existing set value alone
363-
/// value.set_if_not_equals_and(None, Option::is_some);
371+
/// value.set_ne_and(None, Option::is_some);
364372
/// assert_eq!(value, ActiveValue::Set(Some("old")));
365373
///
366374
/// // since Option::is_some(Some("new")) == true, we replace the set value
367-
/// value.set_if_not_equals_and(Some("new"), Option::is_some);
375+
/// value.set_ne_and(Some("new"), Option::is_some);
368376
/// assert_eq!(value, ActiveValue::Set(Some("new")));
369377
/// ```
370-
pub fn set_if_not_equals_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)
378+
pub fn set_ne_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)
371379
where
372380
V: PartialEq,
373381
{
@@ -378,6 +386,14 @@ where
378386
}
379387
}
380388

389+
/// Alias for [`ActiveValue::set_ne_and`]. Kept for compatibility.
390+
pub fn set_if_not_equals_and(&mut self, value: V, f: impl FnOnce(&V) -> bool)
391+
where
392+
V: PartialEq,
393+
{
394+
self.set_ne_and(value, f);
395+
}
396+
381397
/// Get the inner value, unless `self` is [NotSet][ActiveValue::NotSet].
382398
///
383399
/// There's also a panicking version: [ActiveValue::as_ref].

0 commit comments

Comments
 (0)